首页 > 解决方案 > Azure 中具有目录结构的 Blob 的 URL

问题描述

正在使用的程序集:程序集 Microsoft.WindowsAzure.Storage,版本=9.3.1.0

我想要做什么:在我的 Azure 存储中,我将图像存储为 blob,以下列方式

在此处输入图像描述

我想获取所有图像 blob 的 URL 以及它们最后修改的时间戳。

请注意Image1Image4可能具有相同的名称。

我尝试过的

  1. ListBlobsSegmentedAsync(BlobContinuationToken currentToken)我从容器的根目录尝试使用GetDirectoryReference(string relativeAddress),但无法获得所需的结果。

  2. 虽然有点偏离轨道,但我可以通过GetBlockBlobReference(string blobName);

我应该怎么办?

提前致谢。

标签: c#azureazure-blob-storage

解决方案


ListBlobsSegmentedAsync方法有 2 个包含useFlatBlobListing参数的重载。这些重载接受 7 或 8 个参数,我在您的代码中计算了 6 个。

使用以下代码列出容器中的所有 blob。

public static async Task test()
{
    StorageCredentials storageCredentials = new StorageCredentials("xxx", "xxxxx");
    CloudStorageAccount storageAccount = new CloudStorageAccount(storageCredentials, true);
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer container = blobClient.GetContainerReference("container");
    BlobContinuationToken blobContinuationToken = null;
    var resultSegment = await container.ListBlobsSegmentedAsync(
         prefix: null,
         useFlatBlobListing: true,
         blobListingDetails: BlobListingDetails.None,
         maxResults: null,
         currentToken: blobContinuationToken,
         options: null,
         operationContext: null
     );

     // Get the value of the continuation token returned by the listing call.
     blobContinuationToken = resultSegment.ContinuationToken;
     foreach (IListBlobItem item in resultSegment.Results)
     {
          Console.WriteLine(item.Uri);
     }
}

结果如下:

在此处输入图像描述


推荐阅读