【问题标题】:Get blobs from azure storage excluding subfolders从 azure 存储中获取 blob,不包括子文件夹
【发布时间】:2021-10-21 08:13:43
【问题描述】:
尝试使用 CloudBlobContainer.ListBlobs 从 Azure 存储容器获取 blob,但我只想从特定文件夹(使用前缀)获取 blob,而不是主文件夹下的子文件夹。
例如,假设我有这个:
Folder
Sub-Folder
image2.jpg
image1.jpg
如果我使用文件夹作为前缀,我想获取 image1.jpg 并排除 image2.jpg(子文件夹下的任何内容)
【问题讨论】:
标签:
azure-storage
azure-blob-storage
azure-storage-files
【解决方案1】:
来自Reference
如果调用 CloudBlobContainer.listBlobs() 方法,默认情况下会返回一个 BlobItems 列表,其中包含容器下的 blob 和目录。这是 v8 的默认行为。
即使你想匹配模式,正如@Gaurav mantri-reference所提到的那样
Azure Blob 中对服务器端搜索的支持有限
贮存。您唯一可以过滤的是 blob 前缀,即您可以
指示 Azure 存储服务仅返回其中的 blob 名称
以某些字符开头。
但是如果你想根据文件名获取,下面的例子可能会给出一个想法
var container = blobClient.GetContainerReference(containerName);
foreach (var file in container.ListBlobs(prefix: " Folder/filename.xml", useFlatBlobListing: true))
{ …}
在您的情况下,请尝试使用 Folder/filename.jpg。
注意:
- useFlatBlobListing:将此值设置为 true 将确保只返回 blob(包括在其中的任何子文件夹内)
目录),而不是目录和 blob。
- 因此,如果您只需要 blob,则必须将 UseFlatBlobListing 属性选项设置为 true。
其他参考:
Ref 1,ref 2