Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to List the containers in azure blob storage?

I am developing a social network application.

I want to creat ,in azure storage, a container for each user (client) joining a social network, which means user1 has a container named container 1, and inside container 1 there will be user 1 profile in xml format and a profile picture of user 1.

Similarly, for user2 there will be container 2 created in azure blob storage and then user 2 profile is saved in xml format and profile picture of user 2, and it goes like this so for, let say, 10 users there will be 10 containers.

If i want to list all 9 users' information stored in Azure storage in different 9 containers from user client 1, how could i do that?

I am using webservice, but the challenge I am facing is how to collect all the 9 user profile information located in 9 different containers.

like image 524
Kabinad Teshager Avatar asked Dec 10 '11 18:12

Kabinad Teshager


2 Answers

Azure Blob Storage client library v12 for .NET

using Azure;
using Azure.Storage.Blobs;

BlobServiceClient blobServiceClient = new BlobServiceClient(azStorageConnString);
var containerList = blobServiceClient.GetBlobContainers();

foreach (var container in containerList)
  // doImportantWork
like image 104
IdusOrtus Avatar answered Sep 23 '22 19:09

IdusOrtus


For newer versions of Microsoft.WindowsAzure.Storage, use the right variant of CloudBlobClient.ListContainersSegmentedAsync method. Example usage is as follows:

private static async Task<IEnumerable<CloudBlobContainer>> ListContainersAsync(CloudBlobClient cloudBlobClient)
{
    BlobContinuationToken continuationToken = null;
    var containers = new List<CloudBlobContainer>();

    do
    {
        ContainerResultSegment response = await cloudBlobClient.ListContainersSegmentedAsync(continuationToken);
        continuationToken = response.ContinuationToken;
        containers.AddRange(response.Results);

    } while (continuationToken != null);

    return containers;
}

Usage of the above method will be as follows:

string connectionString = "<your connection string>";
CloudStorageAccount.TryParse(connectionString, out CloudStorageAccount storageAccount);

if (storageAccount == null)
{
    Console.WriteLine("Connection string did not work");
}

CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
var containers = await ListContainersAsync(cloudBlobClient);

Alternately, you can create an extension method as follows:

public static class CloudBlobClientExtension
{
    public static async Task<IEnumerable<CloudBlobContainer>> ListContainersAsync(this CloudBlobClient cloudBlobClient)
    {
        BlobContinuationToken continuationToken = null;
        var containers = new List<CloudBlobContainer>();

        do
        {
            ContainerResultSegment response = await cloudBlobClient.ListContainersSegmentedAsync(continuationToken);
            continuationToken = response.ContinuationToken;
            containers.AddRange(response.Results);

        } while (continuationToken != null);

        return containers;
    }
}

Usage of the above extension method will be as follows:

string connectionString = "<your connection string>";
CloudStorageAccount.TryParse(connectionString, out CloudStorageAccount storageAccount);

if (storageAccount == null)
{
    Console.WriteLine("Connection string did not work");
}

CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
IEnumerable<CloudBlobContainer> containers = await cloudBlobClient.ListContainersAsync();

For more details, please refer to MSDN and Balkan's Blog

For older Azure Storage versions, use CloudBlobClient.ListContainers method. Example usage is as follows:

string connectionString = "<your connection string>";
CloudStorageAccount.TryParse(connectionString, out CloudStorageAccount storageAccount);

if (storageAccount == null)
{
    Console.WriteLine("Connection string did not work");
}

CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
var containers = cloudBlobClient.ListContainers();
like image 21
user3613932 Avatar answered Sep 25 '22 19:09

user3613932