如何获取 Blob 存储中容器中所有文件夹的列表?
How to get a list of all folders in an container in Blob Storage?
我正在使用 Azure Blob 存储来存储我的一些文件。我把它们分类在不同的文件夹中。
到目前为止,我可以使用以下命令获取容器中所有 blob 的列表:
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();
}
但是我怎样才能只获取文件夹,然后才可能获取该特定子目录中的文件?
这是在 ASP.NET 核心 btw
编辑:
容器结构如下所示:
Container
|
|
____Folder 1
| ____File 1
| ____File 2
|
|
____Folder 2
____File 3
____File 4
____File 5
____File 6
而不是将 true
作为值传递给 bool useFlatBlobListing
参数,如记录 here 传递 false
。这将只为您提供容器中的顶层子文件夹和 blob
useFlatBlobListing (Boolean)
A boolean value that specifies whether to list blobs in a flat listing, or whether to list blobs hierarchically, by virtual directory.
要进一步减少仅列出顶级文件夹的设置,您可以使用 OfType
public async Task<List<CloudBlobDirectory>> 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();
}
这将 return CloudBlobDirectory 个实例的集合。他们反过来还提供 ListBlobsSegmentedAsync
方法,因此您可以使用该方法获取该目录中的 blob。
顺便说一句,既然你没有真正使用分段,为什么不使用比 ListBlobsSegmentedAsync
更简单的 ListBlobs
方法呢?
为了仅列出容器内的文件夹而不是内容(对象),您可以对 Scala 使用以下内容。这是获取子目录的通用方法,甚至可以用于更棘手的结构,如
Container
|
|
____Folder 1
| ____Folder 11
| ____Folder 12
| |. ____File 111.txt
| |
____Folder 2
____Folder 21
____Folder 22
这里的前缀基本上就是你要查找其子目录的路径。确保将“/”分隔符添加到前缀中。
val container: CloudBlobContainer = blobClient.getContainerReference(containerName)
var blobs = container.listBlobs(prefix + '/' ,false, util.EnumSet.noneOf(classOf[BlobListingDetails]), null, null)
在 listBlobs 函数中,第二个参数用于使用 FlatBlobListing,我们将其设置为 false,因为我们只需要子目录而不是它们的内容。我们可以将其他参数设置为空。
Blob 将包含子目录列表。您可以通过遍历 blob 列表并调用 getUri 函数来获取 URL。
我正在使用 Azure Blob 存储来存储我的一些文件。我把它们分类在不同的文件夹中。
到目前为止,我可以使用以下命令获取容器中所有 blob 的列表:
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();
}
但是我怎样才能只获取文件夹,然后才可能获取该特定子目录中的文件?
这是在 ASP.NET 核心 btw
编辑:
容器结构如下所示:
Container
|
|
____Folder 1
| ____File 1
| ____File 2
|
|
____Folder 2
____File 3
____File 4
____File 5
____File 6
而不是将 true
作为值传递给 bool useFlatBlobListing
参数,如记录 here 传递 false
。这将只为您提供容器中的顶层子文件夹和 blob
useFlatBlobListing (Boolean)
A boolean value that specifies whether to list blobs in a flat listing, or whether to list blobs hierarchically, by virtual directory.
要进一步减少仅列出顶级文件夹的设置,您可以使用 OfType
public async Task<List<CloudBlobDirectory>> 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();
}
这将 return CloudBlobDirectory 个实例的集合。他们反过来还提供 ListBlobsSegmentedAsync
方法,因此您可以使用该方法获取该目录中的 blob。
顺便说一句,既然你没有真正使用分段,为什么不使用比 ListBlobsSegmentedAsync
更简单的 ListBlobs
方法呢?
为了仅列出容器内的文件夹而不是内容(对象),您可以对 Scala 使用以下内容。这是获取子目录的通用方法,甚至可以用于更棘手的结构,如
Container
|
|
____Folder 1
| ____Folder 11
| ____Folder 12
| |. ____File 111.txt
| |
____Folder 2
____Folder 21
____Folder 22
这里的前缀基本上就是你要查找其子目录的路径。确保将“/”分隔符添加到前缀中。
val container: CloudBlobContainer = blobClient.getContainerReference(containerName)
var blobs = container.listBlobs(prefix + '/' ,false, util.EnumSet.noneOf(classOf[BlobListingDetails]), null, null)
在 listBlobs 函数中,第二个参数用于使用 FlatBlobListing,我们将其设置为 false,因为我们只需要子目录而不是它们的内容。我们可以将其他参数设置为空。 Blob 将包含子目录列表。您可以通过遍历 blob 列表并调用 getUri 函数来获取 URL。