get a list of containers in the storage account

with the code:

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                CloudConfigurationManager.GetSetting("<storageaccountname>_AzureStorageConnectionString"));
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("test-blob-container");

I can access the Blob container “test-blob-container”. But is there a way to get a list of containers in the storage account?

Regards Stefan

Answer

ListContainers() method is not supported in .NET Core library, which is by design since it’s not an asynchronous method, but you can still leverage ListContainersSegmentedAsync() to list blob containers in .NET Core library:

var blobClient = storageAccount.CreateCloudBlobClient();
var blobContainers = new List<CloudBlobContainer>();
BlobContinuationToken blobContinuationToken = null;
do
{
    var containerSegment = await blobClient.ListContainersSegmentedAsync(blobContinuationToken);
    blobContainers.AddRange(containerSegment.Results);
    blobContinuationToken = containerSegment.ContinuationToken;
} while (blobContinuationToken != null);

Just like the REST API, segmented results are returned with a continuation token in case that the container listing can’t be finished within only one API call. That’s why ListContainersSegmentedAsync() is the only remaining method in .NET Core – it’s the really asynchronous one which equals to one REST API call.

Leave a Reply

Your email address will not be published. Required fields are marked *