1
votes

I have created blob storage in azure.

Then have created container called as "MyReport"

Inside container "MyReport" I created 2 folders called as "Test" and "Live". Under both folders "Test" and "Live" there are many subfolders.

What I want is to get latest folder created by azure in those folders.

I tried the following:

StorageCredentialsAccountAndKey credentials = new  StorageCredentialsAccountAndKey(accountName, accessKey);
CloudStorageAccount acc = new CloudStorageAccount(credentials, true);
CloudBlobClient client = acc.CreateCloudBlobClient();
CloudBlobDirectory container = client.GetBlobDirectoryReference(@"MyReport/Test");

var folders = container.ListBlobs().Where(b => b as CloudBlobDirectory != null).ToList();

In Folders variable I get many folders but I want to get the latest folder created by azure.

How to do this?

3
As such there're no folders in Azure Blob Storage. They are simply blob prefixes and are part of blob's name. - Gaurav Mantri
@GauravMantri so how to get latest blob prefix in azure context? - James
Are you following a pattern in naming the blobs? - Gaurav Mantri
@GauravMantri yes i am following pattern like DDMMYHYYHHMM-BuildVersion. BuildVersion is string like "1.4.2.123" . But i think its difficult to sort by it - James

3 Answers

1
votes

Update 10/04:

            CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference("test1");
            CloudBlobDirectory myDirectory = cloudBlobContainer.GetDirectoryReference("test");
            var myfiles = myDirectory.ListBlobs(useFlatBlobListing: true, blobListingDetails: BlobListingDetails.All).Where(b => b as CloudBlockBlob != null);

            var my_lastmodified_blob = myfiles.OfType<CloudBlockBlob>().OrderByDescending(b => b.Properties.LastModified).First();
            Console.WriteLine(my_lastmodified_blob.Parent.StorageUri.PrimaryUri.Segments.Last());

The result(there is a "/" at the end of the folder name, you can remove it as per your need):

enter image description here


As per this issue, when list blobs, the blob is ordered by comparing blob's name char-by-char(ascending order).

So in your code, just use ListBlobs method, then use .Last() to fetch the latest one.

Sample code:

#other code

var myblob = container.ListBlobs().Last();
Console.WriteLine(((CloudBlockBlob)myblob).Name);

The result:

enter image description here

1
votes

Actually CloudBlobDirectory don't hold LastModified Date but within folder all CloudBlockBlob hold Last modified date. so We should decide based on inner files

Here is sample and its working for me

CloudBlobClient client = acc.CreateCloudBlobClient();
var container = client.GetContainerReference(@"seleniumtestreports");
CloudBlobDirectory Directory = container.GetDirectoryReference("DevTests");
var BlobFolders = Directory.ListBlobs().OfType<CloudBlobDirectory>()        .Select(f => new { cloudBlobDirectory = f,LastModified = f.ListBlobs().OfType<CloudBlockBlob>().OrderByDescending(dd => dd.Properties.LastModified).FirstOrDefault().Properties.LastModified }).ToList();

var getLastestFolder = BlobFolders.OrderByDescending(s => s.LastModified).FirstOrDefault();
1
votes

With some clue from @Ivan Yang, i found answer for this.

Clue was to use BlobRequest options. So this works for me

StorageCredentialsAccountAndKey credentials = new StorageCredentialsAccountAndKey(accountName, accessKey);

        CloudStorageAccount acc = new CloudStorageAccount(credentials, true);

        CloudBlobClient client = acc.CreateCloudBlobClient();
        CloudBlobDirectory container = client.GetBlobDirectoryReference(@"MyReport/Test");

        BlobRequestOptions options = new BlobRequestOptions();
        options.UseFlatBlobListing = true;
        var listblob = container.ListBlobs(options);

        var latestFolderAzure = listblob.OfType<CloudBlob>().OrderBy(b => b.Properties.LastModifiedUtc).LastOrDefault()?.Parent.Uri.AbsoluteUri;