Skip to content

How to ingegrate

Stefan edited this page Feb 14, 2023 · 2 revisions

With only one storage account

Example Program.cs:

   var builder = WebApplication.CreateBuilder(args);

   builder.Services.AddDefaultAzureBlobStorage(builder.Configuration);

   builder.Services.AddHealthChecks()
      .AddDefaultAzureBlobStorage()

Example appsettings configuration:

  "AzureBlobStorageOptions": {
    "ConnectionString": "DefaultEndpointsProtocol=https;AccountName=xxxxxx;AccountKey=xxxxxx;EndpointSuffix=core.windows.net",
    "ContainerName": "DefaultContainerName"
  },

With multiple storage accounts

The samples above are suitable if you only need one storage account that is connected to your application.

Having multiple storage accounts is also supported by implementing multiple client factories.

   public class MyClientFactory : IMyClientFactory
   {
       private IOptions<MyStorageOptions> Options { get; }

       public MyClientFactory(IOptions<MyStorageOptions> options)
       {
           Options = options;
       }

       public BlobServiceClient GetClient(BlobClientOptions options = null)
       {
           return new BlobServiceClient(Options.Value.ConnectionString, options);
       }

       public string GetDefaultContainerName()
       {
            return Options.Value.ContainerName;
       }
   }

   public interface IMyClientFactory : IAzureBlobStorageClientFactory
   {
   }

The client and a matching service is then registered like that:

   var builder = WebApplication.CreateBuilder(args);

   builder.Services.AddAzureBlobStorage<IMyClientFactory, MyClientFactory>(builder.Configuration);

   builder.Services.AddHealthChecks()
      .AddAzureBlobStorage<IMyClientFactory>()

Clone this wiki locally