使用GetBlobReference()时未初始化Azure存储CloudBlob.Properties

时间:2012-04-03 13:55:17

标签: azure blob azure-storage-blobs

我正在尝试获取有关Azure blob的一些信息(上次修改的UTC日期时间)。此信息存储在CloudBlob.Properties.LastModifiedUtc属性中。

如果我使用方法GetBlobReference()或GetBlockBlobReference(),则不会初始化blob的属性(LastModifiedUtc是DateTime.MinDate)。如果我使用ListBlobs(),则属性被正确初始化(LastModifiedUtc具有正确的值)。

使用GetBlobReference函数时我做错了吗?有没有办法如何为一个特定的blob获取CloudBlob实例?我知道我可以错过ListBlobs()并过滤我感兴趣的blob,或者使用CloudBlobClient类中的ListBlobsWithPrefix(),但是当我要求特定的Blob Reference时,我希望获得所有元数据。

显示我如何使用Azure blob的代码:

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

2 个答案:

答案 0 :(得分:36)

我相信你必须单独调用才能获取属性/元数据。获得blob引用后,请发出以下行以检索属性。

cloudBlob_GetBlobReference.FetchAttributes();

答案 1 :(得分:2)

这与Java SDK有关。但如果拥有CloudBlob派生的CloudBlockBlob对象,您可能需要CloudBlob.downloadAttributes()来电。