【问题标题】:Azure Storage CloudBlob.Properties are not initialized when using GetBlobReference()使用 GetBlobReference() 时未初始化 Azure 存储 CloudBlob.Properties
【发布时间】:2015-03-17 05:59:22
【问题描述】:

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

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

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

显示我如何使用 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));

【问题讨论】:

    标签: azure blob azure-blob-storage


    【解决方案1】:

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

    cloudBlob_GetBlobReference.FetchAttributes();

    【讨论】:

    • 详细说明,GetBlobReference() 不进行任何网络调用。它几乎只是返回一个使用正确 URL 初始化的对象。要获取属性,您需要进行网络调用,而 .FetchAttributes() 是最低限度地做到这一点的方法(执行 HEAD 请求)。
    • 哦,谢谢,我不知道 GetBlobReference() 只是客户端工厂方法,我希望它会调用 Azure,但我很惊讶它没有带来所有数据。现在这对我来说很有意义。
    • 无价的答案让我受益匪浅
    【解决方案2】:

    这与 Java SDK 有关。但是有一个CloudBlob 派生的CloudBlockBlob 对象,你可能需要CloudBlob.downloadAttributes() 调用。

    【讨论】:

      猜你喜欢
      • 2016-02-03
      • 2018-05-11
      • 2018-03-08
      • 2010-09-10
      • 1970-01-01
      • 2014-10-18
      • 2021-01-14
      • 2016-04-03
      • 2017-12-31
      相关资源
      最近更新 更多