Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a list of all the blobs in a container in Azure?

Tags:

c#

cloud

azure

I have the account name and account key of a storage account in Azure. I need to get a list of all the blobs in a container in that account. (The "$logs" container).

I am able to get the information of a specific blob using the CloudBlobClient class but can't figure out how to get a list of all the blobs within the $logs container.

like image 447
SKLAK Avatar asked Aug 17 '15 18:08

SKLAK


People also ask

How do you list blobs in a container in Python?

If you wish to get all the blob names in all the containers in a storage account, just do blob_service. list_containers to iterate through each container and list all blobs under each iteration. This is also a useful article on how to use Azure Blob Storage from Python.

How do I view Azure blob storage?

View a blob container's contentsOpen Storage Explorer. In the left pane, expand the storage account containing the blob container you wish to view. Expand the storage account's Blob Containers. Right-click the blob container you wish to view, and - from the context menu - select Open Blob Container Editor.

What does List_blobs return?

The way list_blobs() work is that it returns an iterator that iterates through all the results doing paging behind the scenes. So simply doing this will get you through all the results, fetching pages as needed: for blob in bucket.list_blobs() print blob.name.


3 Answers

There is a sample of how to list all of the blobs in a container at https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs/#list-the-blobs-in-a-container:

// Retrieve the connection string for use with the application. The storage
// connection string is stored in an environment variable on the machine
// running the application called AZURE_STORAGE_CONNECTION_STRING. If the
// environment variable is created after the application is launched in a
// console or with Visual Studio, the shell or application needs to be closed
// and reloaded to take the environment variable into account.
string connectionString = Environment.GetEnvironmentVariable("AZURE_STORAGE_CONNECTION_STRING");

// Create a BlobServiceClient object which will be used to create a container client
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);

// Get the container client object
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("yourContainerName");

// List all blobs in the container
await foreach (BlobItem blobItem in containerClient.GetBlobsAsync())
{
    Console.WriteLine("\t" + blobItem.Name);
}    
like image 190
Jennifer Marsman - MSFT Avatar answered Oct 17 '22 09:10

Jennifer Marsman - MSFT


Here's the updated API call for WindowsAzure.Storage v9.0:

private static CloudBlobClient _blobClient = CloudStorageAccount.Parse("connectionstring").CreateCloudBlobClient();

public async Task<IEnumerable<CloudAppendBlob>> GetBlobs()
{
    var container = _blobClient.GetContainerReference("$logs");
    BlobContinuationToken continuationToken = null;

    //Use maxResultsPerQuery to limit the number of results per query as desired. `null` will have the query return the entire contents of the blob container
    int? maxResultsPerQuery = null;

    do
    {
        var response = await container.ListBlobsSegmentedAsync(string.Empty, true, BlobListingDetails.None, maxResultsPerQuery, continuationToken, null, null);
        continuationToken = response.ContinuationToken;

        foreach (var blob in response.Results.OfType<CloudAppendBlob>())
        {
            yield return blob;
        }
    } while (continuationToken != null);
}

Update for IAsyncEnumerable

IAsyncEnumerable is now available in .NET Standard 2.1 and .NET Core 3.0

private static CloudBlobClient _blobClient = CloudStorageAccount.Parse("connectionstring").CreateCloudBlobClient();

public async IAsyncEnumerable<CloudAppendBlob> GetBlobs()
{
    var container = _blobClient.GetContainerReference("$logs");
    BlobContinuationToken continuationToken = null;

    //Use maxResultsPerQuery to limit the number of results per query as desired. `null` will have the query return the entire contents of the blob container
    int? maxResultsPerQuery = null;

    do
    {
        var response = await container.ListBlobsSegmentedAsync(string.Empty, true, BlobListingDetails.None, maxResultsPerQuery, continuationToken, null, null);
        continuationToken = response.ContinuationToken;

        foreach (var blob in response.Results.OfType<CloudAppendBlob>())
        {
            yield return blob;
        }
    } while (continuationToken != null);
}
like image 29
Brandon Minnick Avatar answered Oct 17 '22 09:10

Brandon Minnick


Using the new package Azure.Storage.Blobs

BlobServiceClient blobServiceClient = new BlobServiceClient("YourStorageConnectionString");
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("YourContainerName");
var blobs = containerClient.GetBlobs();

foreach (var item in blobs){
    Console.WriteLine(item.Name);
}
like image 8
rcruz Avatar answered Oct 17 '22 09:10

rcruz