簡體   English   中英

如何獲取具有子目錄級別(n 級)的 Blob 容器中的所有 Blob?

[英]How to get hold of all the blobs in a Blob container which has sub directories levels(n levels)?

嘗試使用 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);

ListBlobsSegmentedAsync方法有 2 個包含useFlatBlobListing參數的重載。 這些重載接受 7 或 8 個參數,我在您的代碼中數為 6。 由於參數太多,您可以使用命名參數使代碼更易於理解。

以下代碼已在 .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存儲庫。

使用ListBlobsSegmentedAsync方法的此覆蓋: https : ListBlobsSegmentedAsync並確保為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;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM