如何获取具有子目录级别(n 级)的 Blob 容器中的所有 Blob? [英] How to get hold of all the blobs in a Blob container which has sub directories levels(n levels)?

查看:23
本文介绍了如何获取具有子目录级别(n 级)的 Blob 容器中的所有 Blob?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Tried using the ListBlobsSegmentedAsync method , but this returns only the blobs from the main parent directory level ..

但我需要从所有 n 级子目录中一次性获取完整的 blob 列表.

But I need the entire list of blobs at one go from all the n levels of subdirectories.

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);

推荐答案

ListBlobsSegmentedAsync 方法有 2 个包含 useFlatBlobListing 参数的重载.这些重载接受 7 或 8 个参数,我在您的代码中数为 6.由于参数太多,您可以使用 命名参数 使代码更容易理解.

The ListBlobsSegmentedAsync method has 2 overloads that contain the useFlatBlobListing argument. These overloads accept 7 or 8 arguments, and I count 6 in your code. Because there are so many arguments, you can use named arguments to make the code easier to understand.

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

The code below has been tested successfully in .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.

此代码源自微软的storage-blobs-dotnet-quickstart 存储库.

This code is derived from Microsoft's storage-blobs-dotnet-quickstart repository.

这篇关于如何获取具有子目录级别(n 级)的 Blob 容器中的所有 Blob?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆