2

I'm getting null when I'm trying to fetch LastModified property of Azure Blob, below is the snippet for the same.

CloudStorageAccount storageAccount = CloudStorageAccount.Parse("account");
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("folder");
var blobs = container.ListBlobs();
foreach (var blob in blobs)
{
CloudBlockBlob blockBlob =container.GetBlockBlobReference(blob.ToString());
var timemodified = blockBlob.Properties.LastModified;
}

value fetched in blob above is not a CloudBlobDirectory. Thanks in advance for any help.

Varun Bajpai
  • 545
  • 5
  • 20

2 Answers2

6

Reason you're getting this behavior is because when you execute following line of code:

CloudBlockBlob blockBlob = container.GetBlockBlobReference(blob.ToString());

It essentially creates a new instance of CloudBlockBlob object and it's properties are initialized to the default value. You would need to call FetchAttributes method on this to fill the properties.

Also, when you list the blobs the properties of the blob are fetched as well. So you need not create a new instance of CloudBlockBlob. Simply use the blob object you got as listing result and use the properties from there. So your code would be:

        foreach (var blob in blobs)
        {
            var timemodified = blob.Properties.LastModified;
        }
Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
  • Getting below exception on blockBlob.FetchAttributes() – Varun Bajpai Oct 27 '16 at 10:04
  • An exception of type 'Microsoft.WindowsAzure.Storage.StorageException' occurred in Microsoft.WindowsAzure.Storage.dll but was not handled in user code Additional information: The remote server returned an error: (404) Not Found. – Varun Bajpai Oct 27 '16 at 10:05
  • Instead of using `blob.ToString()`, find out blob's name and use that. Assuming all your blobs are of type `BlockBlob`, you could do something like `container.GetBlockBlobReference((blob as CloudBlockBlob).Name)`. **However, please note that when you call `FetchAttributes` you're making a request to Azure storage to get the information which is already there in your `blob` object.** – Gaurav Mantri Oct 27 '16 at 10:08
  • Although, I'm uncertain of the blob type as it could be any CloubBlockBlob or CloudBlobDirectory. That i can handle it in my code thanks for the help. Cheers :) – Varun Bajpai Oct 27 '16 at 10:17
  • If all you want is the list of blob (and make sure you don't get `CloudBlobDirectory`), you can simply do `var blobs = container.ListBlobs(null, true)`. This will ensure that you don't get back any directories when listing blobs. – Gaurav Mantri Oct 27 '16 at 10:19
  • But this would not get me blobs inside the child BlobDirectory – Varun Bajpai Oct 27 '16 at 10:22
  • That's correct. This will give you a list of all the blobs in all the directories. – Gaurav Mantri Oct 27 '16 at 10:24
1

For those who use a newer version of CloudBlockBlob and FetchAttributes does not exist – use DownloadAttributes instead.

CloudBlockBlob blockBlob = container.GetBlockBlobReference(blob.ToString());
blockBlob.DownloadAttributes();
var timemodified = blockBlob.Properties.LastModified;
basecode
  • 1,490
  • 12
  • 13