0

I'd like to be able to list the 5 oldest blobs within a container.

I'm creating a client:

CloudStorageAccount.TryParse(connection, out CloudStorageAccount storageAccount);
var blobClient = storageAccount.CreateCloudBlobClient();

At this step, I need to grab the 5 oldest blobs in order to copy them to a different container.

How do we list the 5 oldest blobs in a container?

Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
  • Like [this](https://stackoverflow.com/a/45822052/1663001)? But I think this may scan all your blobs so can end up being quite intensive. – DavidG Jul 16 '19 at 15:56
  • that looks pretty good! but what if i have 10,000 blobs in that container? – Alex Gordon Jul 16 '19 at 16:04
  • 1
    Then it will probably scan 10,000 blobs! Blob storage isn't really meant to be used like this, much safer to use another data storage for the indexing. – DavidG Jul 16 '19 at 16:06

1 Answers1

1

As mentioned by @Davidg , you can use OrderByDescending on LastModified date , but it will scan all the files.

CloudBlobContainer rootContainer = blobClient.GetContainerReference("sample");
CloudBlobDirectory dir1;
var items = rootContainer.ListBlobs(id + "/someData/" + somedataid.ToString() + "/", false);

foreach (var blob in items.OfType<CloudBlob>()
    .OrderByDescending(b => b.Properties.LastModified).Skip(1000).Take(500))


 {

}

Since this operation will be time consuming and costly, i would suggest you to store the blob information like BlobName, Blob Url and Last Modified Date etc as per your need.

By that you will have the options to perform any operation on it.

Hope it helps.

Mohit Verma
  • 5,140
  • 2
  • 12
  • 27