【问题标题】:How to get hold of all the blobs in a Blob container which has sub directories levels(n levels)?如何获取具有子目录级别(n 级别)的 Blob 容器中的所有 Blob?
【发布时间】:2015-08-04 09:44:28
【问题描述】:

尝试使用 ListBlobsSegmentedAsync 方法, 但这仅返回主父目录级别的 blob ..

但我需要一次从所有 n 级子目录中获取整个 blob 列表。

BlobContinuationToken continuationToken = null;
bool useFlatBlobListing = true;
BlobListingDetails blobListingDetails = BlobListingDetails.None;
int maxBlobsPerRequest = 500;
var blobOptions = new BlobRequestOptions (true );

do
 {
    var listingResult = await cbDir.ListBlobsSegmentedAsync(useFlatBlobListing, blobListingDetails, maxBlobsPerRequest, continuationToken, null, null);
    continuationToken = listingResult.ContinuationToken;
    srcBlobList.AddRange(listingResult.Results);
 } while (continuationToken != null);

【问题讨论】:

  • 你能分享一下你到目前为止所尝试的吗?当然,还有实际的代码部分。
  • 我刚刚用我使用的代码编辑了我的问题

标签: c# azure blobs


【解决方案1】:

ListBlobsSegmentedAsync 方法有 2 个包含 useFlatBlobListing 参数的重载。这些重载接受 7 或 8 个参数,我在您的代码中计算了 6 个。因为参数太多,可以使用named arguments,让代码更容易理解。

以下代码已在 .NET Core 中测试成功。

BlobContinuationToken blobContinuationToken = null;
do
{
    var resultSegment = await cloudBlobContainer.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);
    }
} while (blobContinuationToken != null); // Loop while the continuation token is not null.

此代码源自 Microsoft 的 storage-blobs-dotnet-quickstart 存储库。

【讨论】:

  • 这很有帮助。
【解决方案2】:

使用此 ListBlobsSegmentedAsync 方法的覆盖:https://msdn.microsoft.com/en-us/library/dn434672.aspx 并确保为 useFlatBlobListing 参数传递 true。这将列出所有子目录中的所有 blob。

更新

这是我使用的代码,它返回该子文件夹中的 blob 以及该子文件夹中的所有子文件夹。

    /// <summary>
    /// Code to fetch blobs from "temp" folder inside "blah" blob container.
    /// </summary>
    private static void GetFilesInSubfolder()
    {
        var account = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
        var blobClient = account.CreateCloudBlobClient();
        var container = blobClient.GetContainerReference("blah");
        var directory = container.GetDirectoryReference("temp");
        var result = directory.ListBlobsSegmented(true, BlobListingDetails.None, 500, null, null, null);
        var blobs = result.Results;
    }

【讨论】:

  • 没有 gaurav,试过了,但它再次只返回父目录中的 blob,而不返回其他子目录中的 blob。
  • 请用代码更新您的问题。这样会更容易识别问题。
  • 我尝试运行您的代码,它对我有用。我已经更新了我的答案并包含了我的代码。
  • 尝试遍历子目录并实现了我想要的。无论如何感谢 Gaurav 的建议
  • 但现在我的问题是,它在复制 110 MB 的 blob 对象后出现超时,说“客户端无法在指定的超时时间内完成操作。”。我无法找出增加此操作超时的解决方案。任何帮助都会非常有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-31
  • 1970-01-01
相关资源
最近更新 更多