15

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);
HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
NITHESHKUMAR R
  • 149
  • 1
  • 1
  • 8

2 Answers2

23

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.

Chris Koester
  • 492
  • 4
  • 9
16

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;
    }
Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
  • No gaurav, tried it but again it gives back only the blobs from the parent directory and not from the other sub directories. – NITHESHKUMAR R May 22 '15 at 12:08
  • Please update your question with the code. It will be easier that way to identify the issue. – Gaurav Mantri May 22 '15 at 12:14
  • I tried running your code and it worked for me. I have updated my answer and included my code. – Gaurav Mantri May 22 '15 at 15:41
  • Tried iterating through the sub directories and achieved what i wanted. Thanks anyways Gaurav for your suggestions – NITHESHKUMAR R May 26 '15 at 07:39
  • 1
    But now my issue is ,its getting timeout after copying 110 MB of blob objects saying "The client could not finish the operation within specified timeout.". Im unable to figure out the solution in increasing the timeout for this operation. Any help would be very helpful – NITHESHKUMAR R May 26 '15 at 07:42
  • @NITHESHKUMARR do you have a complete solution now? – kudlatiger May 14 '20 at 11:39
  • @GauravMantri-AIS Please provide full source. i get below info for blob "Id = 9, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}"" – kudlatiger May 14 '20 at 11:50