44

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();
1
  • 3
    Does your blob container contain just block blobs? If that's the case, then you could simply do something like: List<string> blobNames = list.Select(b => (b as CloudBlockBlob).Name);. Commented May 6, 2014 at 5:21

10 Answers 10

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();
Sign up to request clarification or add additional context in comments.

4 Comments

We can get some additional info like Size, Modified date and more. Refer my answer at bottom.
What is list?
list is found in askers post above.
Answer outdated - ListBlobs not available anymore - see this answer: stackoverflow.com/a/59474285/10133085
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?

1 Comment

I think list should be updated to blobs. i.e. var blobs = backupContainer.ListBlobs(useFlatBlobListing: true);
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);

Comments

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();

2 Comments

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.
I am glad I could help.
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();

7 Comments

What is continuationToken??
BlobContinuationToken continuationToken = null;
@PetraStručić please provide some information, the comment you gave is not helpful!
@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.
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);
|
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.

Comments

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;
    }

2 Comments

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.
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.
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
    }
}

2 Comments

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.
What is list?
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

Comments

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;
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.