Shantanu
Shantanu

Reputation: 69

how to list top 100 blobs from azure blob storage using c#

I am using this code and could get the result but dont think this is a proper way

CloudBlobDirectory blobDirectory = 
container.GetDirectoryReference(blobDirectoryReference);

IEnumerable<IListBlobItem> blobList =  
blobDirectory.ListBlobs(useFlatBlobListing:true, 
blobListingDetails:BlobListingDetails.Metadata)
.OfType<CloudBlockBlob>()
.OrderByDescending(m=>m.Properties.LastModified).Take(100);

Upvotes: 2

Views: 1507

Answers (1)

Peter Bons
Peter Bons

Reputation: 29780

That is correct, it does not get the top 100 server side. For that you need the ListBlobsSegmented method.

At it simplest it can look like this:

IEnumerable<IListBlobItem> blobList = container.ListBlobsSegmented(string.Empty, true,
    BlobListingDetails.Metadata, 100, null, null, null).Results
    .OfType<CloudBlockBlob>()
    .OrderByDescending(m => m.Properties.LastModified);

You can split the query in smaller chuncks and then use a BlobContinuationToken to iterate over the chunks. Look at the documentation I provided in this post for all the options.

Do mind however that it is not possible to sort the results serverside. It wil only sort the chunk you retrieve and client side only.

Upvotes: 2

Related Questions