Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a list of all folders in an container in Blob Storage?

I am using Azure Blob Storage to store some of my files away. I have them categorized in different folders.

So far I can get a list of all blobs in the container using this:

    public async Task<List<Uri>> GetFullBlobsAsync()
    {
        var blobList = await Container.ListBlobsSegmentedAsync(string.Empty, true, BlobListingDetails.None, int.MaxValue, null, null, null);

        return (from blob in blobList.Results where !blob.Uri.Segments.LastOrDefault().EndsWith("-thumb") select blob.Uri).ToList();
    }

But how can I only get the folders, and then maybe the files in that specific subdirectory?

This is on ASP.NET Core btw

EDIT:

Container structure looks like this:

Container  
|  
|  
____Folder 1  
|   ____File 1  
|   ____File 2  
|   
|  
____Folder 2   
    ____File 3  
    ____File 4  
    ____File 5  
    ____File 6  
like image 238
user2657943 Avatar asked May 26 '17 15:05

user2657943


People also ask

Does azure Blob Storage have folders?

Blob containers contain blobs and folders (that can also contain blobs). The following steps illustrate how to view the contents of a blob container within Storage Explorer: Open Storage Explorer. In the left pane, expand the storage account containing the blob container you wish to view.

How do I access blob storage containers?

Navigate to your storage account overview in the Azure portal. Under Data storage on the menu blade, select Blob containers. Select the containers for which you want to set the public access level. Use the Change access level button to display the public access settings.

What is difference between container and blob?

A container organizes a set of blobs, similar to a directory in a file system. A storage account can include an unlimited number of containers, and a container can store an unlimited number of blobs.


1 Answers

Instead of passing true as the value to the bool useFlatBlobListing parameter as documented here pass false. That will give you only the toplevel subfolders and blobs in the container

useFlatBlobListing (Boolean)

A boolean value that specifies whether to list blobs in a flat listing, or whether to list blobs hierarchically, by virtual directory.

To further reduce the set to list only toplevel folders you can use OfType

    public async Task<List<Cloud​Blob​Directory>> GetFullBlobsAsync()
    {
        var blobList = await Container.ListBlobsSegmentedAsync(string.Empty, false, BlobListingDetails.None, int.MaxValue, null, null, null);

        return (from blob in blobList
                             .Results
                             .OfType<CloudBlobDirectory>() 
                select blob).ToList();
    }

This will return a collection of Cloud​Blob​Directory instances. They in turn also provide the ListBlobsSegmentedAsync method so you can use that one to get the blobs inside that directory.

By the way, since you are not really using segmentation why not using the simpler ListBlobs method than ListBlobsSegmentedAsync?

like image 54
Peter Bons Avatar answered Oct 26 '22 15:10

Peter Bons