如何在Blob存储中获取容器中所有文件夹的列表?

问题描述:

我正在使用Azure Blob存储来存储我的一些文件.我将它们归类在不同的文件夹中.

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?

这是在ASP.NET Core btw上

This is on ASP.NET Core btw

容器结构如下:

Container  
|  
|  
____Folder 1  
|   ____File 1  
|   ____File 2  
|   
|  
____Folder 2   
    ____File 3  
    ____File 4  
    ____File 5  
    ____File 6  

而不是按照这里通过.这样只会给您容器中的顶级子文件夹和Blob

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(布尔值)

useFlatBlobListing (Boolean)

一个布尔值,它指定是按虚拟目录在平面列表中列出Blob,还是按层次结构列出Blob.

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

要进一步减少设置以仅列出顶级文件夹,可以使用OfType

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.

顺便说一句,既然您没有真正使用细分,为什么不使用比ListBlobsSegmentedAsync更简单的ListBlobs方法?

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