4
votes

I want all the names from the Azure Blob files in a container (for a certain purpose). I found some similar questions, even one on stackoverflow.com (getting list of names of Azure blob files in a container?). But all those answers are using the ListBlobs()-method first. That is what I want to prevent, beacause with 24,000 files, it takes about 10 minutes before all the files are stored in a list and the name is received from every file. Method I use now in C#:

            var container = GetOrCreateBlobContainer(containerName);
            var blobs = container.ListBlobs(useFlatBlobListing: true);
            var blobNames = blobs.OfType<CloudBlockBlob>().Select(b => b.Name).ToList();
            return blobNames;

What I want: Get all names directly from the storage without using container.ListBlobs() first.

Is this possible??

3

3 Answers

5
votes

What I want: Get all names directly from the storage without using container.ListBlobs() first.

Is this possible??

No, it is not possible. You will need to call ListBlobs to get a list of all blobs in a container.

3
votes

For people who have the same problem, I want to clear some things. This is the method which takes 10 minutes. This is because of the fact we are looping through all the 24,000 files to get the names:

public IEnumerable<string> GetAllBlobNames(string containerName)
{
    try
    {
        var container = GetOrCreateBlobContainer(containerName);
        var blobs = container.ListBlobs(useFlatBlobListing: true);

        var blobNames = new List<string>();
        foreach (var item in blobs)
        {
            var blob = (CloudBlockBlob)item;
            blob.FetchAttributes();
            blobNames.Add(blob.Name);
        }
        return blobNames;
    }
    catch (Exception ex)
    {
        throw new DomainException("BlobStorageProvider_Retrieve_Fails", typeof(Properties.Resources), containerName, ex.ToStringExtended());
    }
}

After some research we changed the code to:

var container = GetOrCreateBlobContainer(containerName);
var blobs = container.ListBlobs(useFlatBlobListing: true);
var blobNames = blobs.OfType<CloudBlockBlob>().Select(b => b.Name).ToList();
return blobNames;

This second way has a duration of 2 seconds. Which is a lot faster than 10 minutes. But we were still wondering if we had to use the container.ListBlobs().

As Gaurav Mantri says, it's impossible to get all the names directly from the storage without using container.ListBlobs() first. Hope this helps other people too ;)

0
votes

As Gaurav Mantri says you need to call ListBlobs to get a list of all blobs in a container firstly.

Here is a workaround method, I suggest you use container’s metadata or upload a txt file to store all the blob name, then you could download the metadata or a txt file directly to get all the blobs name.

Note: the metadata max size is 8kb, if you have many files, I suggest you could use the txt to store the file name.