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

匆匆过客 提交于 2019-12-17 16:46:20

问题


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

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

回答1:


Use this override of ListBlobsSegmentedAsync method: https://msdn.microsoft.com/en-us/library/dn434672.aspx and make sure that you pass true for useFlatBlobListing parameter. This will list all blobs from all subdirectories.

UPDATE

This is the code I have used and it returns me the blobs in that subfolder and all subfolders inside that subfolder.

    /// <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;
    }



回答2:


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.

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.

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



来源:https://stackoverflow.com/questions/30395502/how-to-get-hold-of-all-the-blobs-in-a-blob-container-which-has-sub-directories-l

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!