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

前端 未结 2 1697
感情败类
感情败类 2021-01-12 09:54

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 us

相关标签:
2条回答
  • 2021-01-12 10:31

    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?

    0 讨论(0)
  • 2021-01-12 10:36

    In order to list only the folders inside a Container and not the content(objects), you can use the following for Scala. It's a generic way to get the subdirectories and can work for even trickier structures like

    Container  
    |  
    |  
    ____Folder 1  
    |   ____Folder 11  
    |   ____Folder 12  
    |   |.  ____File 111.txt
    |   |   
    ____Folder 2   
        ____Folder 21 
        ____Folder 22  
    

    Here prefix is basically the path whose subdirectories you wish to find. Make sure the '/' delimiter is added to the prefix.

    val container: CloudBlobContainer = blobClient.getContainerReference(containerName)
    var blobs = container.listBlobs(prefix + '/' ,false, util.EnumSet.noneOf(classOf[BlobListingDetails]), null, null)  
    

    In listBlobs function, the second argument is for using FlatBlobListing and we set that to false since we only need the subdirectories and not their content. The other arguments we can set as null. Blobs will contain the list of subdirectories. You can get the URL by iterating over the list of blobs and calling the getUri function.

    0 讨论(0)
提交回复
热议问题