Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Storage CloudBlob.Properties are not initialized when using GetBlobReference()

I'm trying to get some information about Azure blob (last modified UTC date time). This information is stored CloudBlob.Properties.LastModifiedUtc property.

If I use method GetBlobReference() or GetBlockBlobReference(), the Properties of the blob are not initialized (LastModifiedUtc is DateTime.MinDate). If I use ListBlobs() the Properties are initialized correctly (LastModifiedUtc has correct value).

Am I doing something wrong when using GetBlobReference function? Is there some way how to get CloudBlob instance just for one specific blob? I know I can missue ListBlobs() and filter just the blob I'm interested in, or use ListBlobsWithPrefix() from class CloudBlobClient, but I would expect to get all the metadata when I ask for specific Blob Reference.

Code showing how I'm working with Azure blobs:

    string storageAccountName = "test";
    string storageAccountKey = @"testkey";
    string blobUrl = "https://test.blob.core.windows.net";
    string containerName = "testcontainer";
    string blobName = "testbontainer";

    var credentials = new StorageCredentialsAccountAndKey(storageAccountName, storageAccountKey);
    var cloudBlobClient = new CloudBlobClient(blobUrl, credentials);
    var containerReference = cloudBlobClient.GetContainerReference(string.Format("{0}/{1}", blobUrl, containerName));

    // OK - Result is of type CloudBlockBlob, cloudBlob_ListBlobs.Properties.LastModifiedUtc > DateTime.MinValue
    var cloudBlob_ListBlobs = containerReference.ListBlobs().Where(i => i is CloudBlob && ((CloudBlob)i).Name == blobName).FirstOrDefault() as CloudBlob;

    // WRONG - Result is of type CloudBlob, cloudBlob_GetBlobReference.Properties.LastModifiedUtc == DateTime.MinValue
    var cloudBlob_GetBlobReference = containerReference.GetBlobReference(string.Format("{0}/{1}/{2}", blobUrl, containerName, blobName));

    // WRONG - Result is of type CloudBlockBlob, cloudBlob_GetBlockBlobReference.Properties.LastModifiedUtc == DateTime.MinValue
    var cloudBlob_GetBlockBlobReference = containerReference.GetBlockBlobReference(string.Format("{0}/{1}/{2}", blobUrl, containerName, blobName));
like image 725
Tiny Avatar asked Apr 03 '12 13:04

Tiny


2 Answers

I believe you have to make a seperate call to fetch the attributes/metadata. After you have the blob referrence, issue the following line to retrieve the attributes.

cloudBlob_GetBlobReference.FetchAttributes();

like image 151
BrentDaCodeMonkey Avatar answered Sep 19 '22 04:09

BrentDaCodeMonkey


This relates to the Java SDK. But having a CloudBlob derived CloudBlockBlob object, you may need the CloudBlob.downloadAttributes() call.

like image 23
kervin Avatar answered Sep 22 '22 04:09

kervin