Getting list of names of Azure blob files in a container?

81,303

Solution 1

If you're using Windows Azure Storage 4.3.0, try this code.

List<string> blobNames = list.OfType<CloudBlockBlob>().Select(b => b.Name).ToList();

Solution 2

Here is one more way to get this done:

CloudStorageAccount backupStorageAccount = CloudStorageAccount.Parse(blobConectionString);

var backupBlobClient = backupStorageAccount.CreateCloudBlobClient();
var backupContainer = backupBlobClient.GetContainerReference(container);

// useFlatBlobListing is true to ensure loading all files in
// virtual blob sub-folders as a plain list
var list = backupContainer.ListBlobs(useFlatBlobListing: true);
var listOfFileNames = new List<string>();

foreach (var blob in blobs) {
  var blobFileName = blob.Uri.Segments.Last();
  listOfFileNames.Add(blobFileName); 
}

return listOfFileNames;

Source: How to load list of Azure blob files recursively?

Solution 3

We can get some additional info like Size, Modified date and Name.

CloudStorageAccount backupStorageAccount = CloudStorageAccount.Parse(YOUR_CON_STRING);

var backupBlobClient = backupStorageAccount.CreateCloudBlobClient();
var backupContainer = backupBlobClient.GetContainerReference("CONTAINER");


var blobs = backupContainer.ListBlobs().OfType<CloudBlockBlob>().ToList();

foreach (var blob in blobs)
{
    string bName = blob.Name;
    long bSize = blob.Properties.Length;
    string bModifiedOn = blob.Properties.LastModified.ToString();        
}

Also you can download a specific file by Name.

 // Download file by Name
 string fileName = "Your_file_name";
 CloudBlockBlob blobFile = backupContainer.GetBlockBlobReference(fileName);
 blobFile.DownloadToFile(@"d:\"+ fileName, System.IO.FileMode.Create);

Solution 4

This works with WindowsAzure.Storage 9.3.3.

CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
var cloudBlobContainer = cloudBlobClient.GetContainerReference(containerName);

var blobResultSegment = await cloudBlobContainer.ListBlobsSegmentedAsync(continuationToken);
var blobs = blobResultSegment.Results.Select(i => i.Uri.Segments.Last()).ToList();

Solution 5

Full answer with details.

        // Parse the connection string and return a reference to the storage account.
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("AzureBlobConnectionString"));

        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

        // Retrieve reference to a previously created container.
        CloudBlobContainer container = blobClient.GetContainerReference("container_name");

        // Retrieve reference to a blob named "test.csv"
        CloudBlockBlob blockBlob = container.GetBlockBlobReference("BlobName.tex");

        //Gets List of Blobs
        var list = container.ListBlobs();
        List<string> blobNames = list.OfType<CloudBlockBlob>().Select(b => b.Name).ToList();
Share:
81,303
Toubi
Author by

Toubi

Updated on July 09, 2022

Comments

  • Toubi
    Toubi almost 2 years

    I need to list names of Azure Blob file names. Currently I m able to list all files with URL but I just need list of names. I want to avoid parsing names. Can you please see my below code and guide:

    CloudStorageAccount backupStorageAccount = CloudStorageAccount.Parse(blobConectionString);
    
    var backupBlobClient = backupStorageAccount.CreateCloudBlobClient();
    var backupContainer = backupBlobClient.GetContainerReference(container);
    
    var list = backupContainer.ListBlobs();
    
  • Toubi
    Toubi about 10 years
    thanks. Can it be done in Lambda/ Linq please ? Also just thinking this way it will call GetBlobProperties function for every blob file right ? Your advice please.
  • Vignesh Raja
    Vignesh Raja about 6 years
    We can get some additional info like Size, Modified date and more. Refer my answer at bottom.
  • Rashik Hasnat
    Rashik Hasnat about 6 years
    I was facing problems regarding blobs with a space in its name. I was using the absolute uri and it wasn't serving my purpose. This answer solved the problem for me.
  • mwilson
    mwilson over 5 years
    What is continuationToken??
  • mwilson
    mwilson over 5 years
    What is list?
  • mwilson
    mwilson over 5 years
    What is list?
  • Petra Stručić
    Petra Stručić over 5 years
    BlobContinuationToken continuationToken = null;
  • NorwegianClassic
    NorwegianClassic about 5 years
    list is found in askers post above.
  • Raven
    Raven about 5 years
    @PetraStručić please provide some information, the comment you gave is not helpful!
  • Petra Stručić
    Petra Stručić about 5 years
    @Peter please provide some information, the comment you gave is not helpful! On a more serious note, I think my comment is self-explanatory regarding the context of this whole thread, but can you ask a specific question? I would like to improve my comment if possible.
  • Raven
    Raven about 5 years
    It's not clear what you mean by just commenting a line of code without writing what it's for... "The property is null if there are no more items to fetch" would have been motch more helpful and clear.
  • Petra Stručić
    Petra Stručić about 5 years
    I focused on the scope of the original question, but sure. Since getting all blobs can be a heavy operation to do, it's good to granulate it in smaller chunks with maxResults parameter. ContinuationToken tracks the number of records left for listing. In this concrete code example of mine, its potential is not used. Here's an example of its use: do { var response = await ListBlobsSegmentedAsync(continuationToken); continuationToken = response.ContinuationToken; results.AddRange(response.Results); } while (continuationToken != null);
  • Alex Gordon
    Alex Gordon almost 5 years
    how would you iterate through all containers and all the blobs in those containers?
  • Apollo
    Apollo about 4 years
    I am glad I could help.
  • Kavo
    Kavo almost 4 years
    I think list should be updated to blobs. i.e. var blobs = backupContainer.ListBlobs(useFlatBlobListing: true);
  • NoChance
    NoChance over 3 years
    Thanks for your answer. I am not setup on Azure yet, but I was wondering, if you know, how fast is listing say, 1000 blob names? I know it depends on several factors, but just a very general estimate would help me. Thanks.
  • Jonathan Allen
    Jonathan Allen over 3 years
    I don't remember, other than it was fast enough to not annoy me. I only have a few hundred objects, but I can't imagine you'll run into any problems with a thousand.
  • Adam Kuzański
    Adam Kuzański about 3 years
    Answer outdated - ListBlobs not available anymore - see this answer: stackoverflow.com/a/59474285/10133085