skip to Main Content

I have an application which i would like to make reference to two different storage accounts (SA1 and SA2) which both has a single contaner each "container1" and "container2" respectively. I have the below DI setup for both (Not sure this is correct)

        serviceCollection.AddAzureClients(builder =>
    {
        builder.AddBlobServiceClient("SA1_connectionStr"))
            .WithVersion(BlobClientOptions.ServiceVersion.V2019_02_02);

       builder.AddBlobServiceClient("SA2_connectionStr"))
            .WithVersion(BlobClientOptions.ServiceVersion.V2019_02_02);

    });

When i use the "blobContainer" reference (using the code below) it’s only refering the container in the "SA2" storgaeAccount (maybe bcause it was added last in the DI setup)

 private readonly BlobServiceClient blobServiceClient;
 var blobContainer = blobServiceClient.GetBlobContainerClient(container2);

How do i make reference to storageAccount SA1 and storageAccount SA2 seperately?

2

Answers


  1. You’re going to want to the name the clients in order to differentiate them:

       serviceCollection.AddAzureClients(builder =>
        {
            builder.AddBlobServiceClient("SA1_connectionStr"))
                .WithName("SA1")
                .WithVersion(BlobClientOptions.ServiceVersion.V2019_02_02);
    
           builder.AddBlobServiceClient("SA2_connectionStr"))
                .WithName("SA2")
                .WithVersion(BlobClientOptions.ServiceVersion.V2019_02_02);
    
        });
    

    and then you can access them by injecting:

    IAzureClientFactory<BlobServiceClient> clientFactory
    

    into your constructor and then

    _sa1Client = clientFactory.CreateClient("SA1");
    _sa2Client = clientFactory.CreateClient("SA2");
    

    and then use those clients to actually interact with the different containers.

    You can checkout the official documentation for this here.

    Login or Signup to reply.
  2. alternitive to akseli

     public void ConfigureServices(IServiceCollection services)
            {
                services.AddBlobServiceClient("SA1", options =>
                {
                    options.ConnectionString = "your_connection_string_here_1";
                });
    
                services.AddBlobServiceClient("SA2", options =>
                {
                    options.ConnectionString = "your_connection_string_here_2";
                });
    
                // Other service registrations...
            }
    
            // Other methods...
        }
    
    public class YourService
        {
            private readonly BlobServiceClient _blobClientSA1;
            private readonly BlobServiceClient _blobClientSA2;
    
            public YourService(IServiceProvider serviceProvider)
            {
                // Inject the specific instance you want
                _blobClientSA1 = serviceProvider.GetRequiredService<BlobServiceClient>("SA1");
                    _blobClientSA2 = serviceProvider.GetRequiredService<BlobServiceClient>("SA2");
            }
    
            // Use _blobClient as needed...
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search