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

前端 未结 2 1714
感情败类
感情败类 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条回答
  •  旧时难觅i
    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> GetFullBlobsAsync()
        {
            var blobList = await Container.ListBlobsSegmentedAsync(string.Empty, false, BlobListingDetails.None, int.MaxValue, null, null, null);
    
            return (from blob in blobList
                                 .Results
                                 .OfType() 
                    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?

提交回复
热议问题