【问题标题】:Find all blobs below a certain path in Azure blob storage在 Azure Blob 存储中查找特定路径下的所有 Blob
【发布时间】:2020-08-17 05:01:48
【问题描述】:

我正在尝试列出以特定路径和文件名开头的所有 blob。

我有这个路径:"files/folder1/folder2/abcdef"

这些文件存在

`"files/folder1/folder2/abcdef_100_100.jpg"`
`"files/folder1/folder2/abcdef_200_200.jpg"`
`"files/folder1/folder2/abcdef_800_600.jpg"`

请注意,它们都以相同的开头,只是文件名的结尾不同。我不知道有什么结局,只是很可能不止一个。

对于具有完整路径的单个文件,此代码效果很好。

CloudBlockBlob cloudBlockBlob = _blobContainer.GetBlockBlobReference(fileName);

到目前为止,我已经尝试使用以下代码获取所有与文件名和文件名开头匹配的文件,但失败了:

var imageFiles = _blobContainer.ListBlobs(startingPath, true, BlobListingDetails.None);

var blobDirectory = _blobContainer.GetDirectoryReference(startingPath);
var imageFiles = blobDirectory .ListBlobs();

这两个都返回一个空结果。

我应该如何做到这一点?

附:数据不得在客户端过滤。将有数千个文件。

P.P.S 如果我必须过滤最后一个文件夹 (folder2) 的内容,那该级别的每个文件夹中的文件将少于 100 个。

更新 1 这也不起作用。

// returns files/folder1/folder2
var dirPath = System.IO.Path.GetDirectoryName(startingPath); 

var ss = _blobContainer.GetDirectoryReference(dirPath);
var imageFiles = ss.ListBlobs();

【问题讨论】:

  • 如果将startingPath 设置为仅包含files/folder1/folder2/ 会怎样?有用吗?

标签: azure azure-blob-storage


【解决方案1】:

ListBlobs 应该按您期望的方式工作。确保您没有区分大小写的问题,并且容器名称的值正确。

完整示例:

var acc = Microsoft.Azure.Storage.CloudStorageAccount.Parse("connstring");
var blobClient = acc.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("files"); // might also be different in your case, not sure with the info in the question
var blobs = container.ListBlobs("folder1/folder2/abcdef", true); // make sure to not put the container name here
foreach (var blob in blobs)
{
    Console.WriteLine(blob.Uri);
}

【讨论】:

  • 成功了。我在路径前面有一个斜线,它似乎很挑剔,无法解决。得到了我想要的所有文件。谢谢。
猜你喜欢
  • 2020-05-21
  • 1970-01-01
  • 2019-07-28
  • 2018-12-24
  • 2015-08-22
  • 2016-09-01
  • 1970-01-01
  • 2013-07-05
  • 2014-04-18
相关资源
最近更新 更多