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

大兔子大兔子 提交于 2019-12-03 16:05:02

问题


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));

回答1:


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();




回答2:


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



来源:https://stackoverflow.com/questions/9994777/azure-storage-cloudblob-properties-are-not-initialized-when-using-getblobreferen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!