43

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();
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Toubi
  • 2,469
  • 10
  • 33
  • 49
  • 3
    Does your blob container contain just block blobs? If that's the case, then you could simply do something like: `List blobNames = list.Select(b => (b as CloudBlockBlob).Name);`. – Gaurav Mantri May 06 '14 at 05:21

10 Answers10

36

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

List<string> blobNames = list.OfType<CloudBlockBlob>().Select(b => b.Name).ToList();
feltocraig
  • 729
  • 7
  • 7
16

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?

Community
  • 1
  • 1
mikhail-t
  • 4,103
  • 7
  • 36
  • 56
  • 2
    I think `list` should be updated to `blobs`. i.e. `var blobs = backupContainer.ListBlobs(useFlatBlobListing: true);` – Kavo Aug 05 '20 at 12:28
15

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);
Vignesh Raja
  • 560
  • 6
  • 10
12

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();
Apollo
  • 1,990
  • 12
  • 44
  • 65
12

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();
Petra Stručić
  • 131
  • 2
  • 7
  • What is `continuationToken`?? – mwilson Jan 25 '19 at 00:07
  • 3
    `BlobContinuationToken continuationToken = null;` – Petra Stručić Jan 28 '19 at 11:53
  • 2
    @PetraStručić please provide some information, the comment you gave is not helpful! – Peter Apr 16 '19 at 10:57
  • 1
    @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. – Petra Stručić Apr 17 '19 at 05:37
  • 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. – Peter Apr 17 '19 at 05:53
  • 1
    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);` – Petra Stručić Apr 17 '19 at 10:35
  • how would you iterate through all containers and all the blobs in those containers? – Alex Gordon Jul 10 '19 at 19:05
8

Update:

Getting list of names of Azure blob files with Azure.Storage.Blobs v12 - Package

var storageConnectionString = "DefaultEndpointsProtocol=...........=core.windows.net";
var blobServiceClient = new BlobServiceClient(storageConnectionString);

//get container
var container = blobServiceClient.GetBlobContainerClient("container_name");

List<string> blobNames = new List<string>();

//Enumerating the blobs may make multiple requests to the service while fetching all the values
//Blobs are ordered lexicographically by name
//if you want metadata set BlobTraits - BlobTraits.Metadata
var blobs = container.GetBlobsAsync(BlobTraits.None, BlobStates.None);
await foreach (var blob in blobs)
{
    blobNames.Add(blob.Name);
}

There are more option and example you can find it here.

This is the link to the nuget package.

Thiago Custodio
  • 17,332
  • 6
  • 45
  • 90
helix-nebula
  • 266
  • 4
  • 5
6

The ListBlobs method doesn't appear to exist anymore. Here is an async verison.

    public static async Task<List<string>> ListBlobNamesAsync(CloudBlobContainer container)
    {
        var blobs = await ListBlobsAsync(container);
        return blobs.Cast<CloudBlockBlob>().Select(b => b.Name).ToList();

        //Alternate version
        //return blobs.Select(b => b.Uri.ToString()).Select(s => s.Substring(s.LastIndexOf('/') + 1)).ToList();
    }

    public static async Task<List<IListBlobItem>> ListBlobsAsync(CloudBlobContainer container)
    {
        BlobContinuationToken continuationToken = null; //start at the beginning
        var results = new List<IListBlobItem>();
        do
        {
            var response = await container.ListBlobsSegmentedAsync(continuationToken);
            continuationToken = response.ContinuationToken;
            results.AddRange(response.Results);
        }

        while (continuationToken != null); //when this is null again, we've reached the end
        return results;
    }
Jonathan Allen
  • 68,373
  • 70
  • 259
  • 447
  • 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. – NoChance Sep 13 '20 at 13:13
  • 1
    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. – Jonathan Allen Sep 14 '20 at 14:18
2

You can access the BlobProperties to get the name:

foreach (object o in list)
{
    BlobProperties bp = o as BlobProperties;
    if (bp != null)
    {
        BlobProperties p = _Container.GetBlobProperties(bp.Name);
        var name = p.Name; // get the name
    }
}
Brendan Green
  • 11,676
  • 5
  • 44
  • 76
2
  CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
   CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
    CloudFileShare share = fileClient.GetShareReference(ConfigurationManager.AppSettings["ShareReference"]);
    if (share.Exists())
    {
          CloudFileDirectory rootDir = share.GetRootDirectoryReference();
          CloudFileDirectory sampleDir = rootDir.GetDirectoryReference("paths");
          if (sampleDir.Exists())
          {
                IEnumerable<IListFileItem> fileList = sampleDir.ListFilesAndDirectories();
                 //CloudFile file = sampleDir.GetFileReference(FileName + ext);
                 //return file;
           }
           return null;
      }

From fileList one can get all the files from azure file

Beena Gupta
  • 178
  • 12
0

We have to use ListBlobsSegmentedAsync() method, Then we can find out the blob by the following code :

public CloudBlockBlob GetLatestBlobByBlobNamePattern(CloudBlobContainer container, string blobNamePattern) 
        {
            var root = container.GetDirectoryReference(string.Empty);
            var blobsList = root.ListBlobsSegmentedAsync(null);
            blobsList.Wait();
            BlobResultSegment segment = blobsList.Result;
            List<IListBlobItem> list = new List<IListBlobItem>();
            list.AddRange(segment.Results);
            while (segment.ContinuationToken != null)
            {
                var blobs = container.ListBlobsSegmentedAsync(segment.ContinuationToken);
                blobs.Wait();
                segment = blobs.Result;
                list.AddRange(segment.Results);
            }
            var blob = list.Where(x => x.Uri.Segments.Last().Contains(blobNamePattern)).FirstOrDefault();
            return (CloudBlockBlob)blob;
        }
Simran Kaur
  • 217
  • 1
  • 5
  • 15