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

Tin*_*iny 15 blob azure 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));
Run Code Online (Sandbox Code Playgroud)

Bre*_*key 37

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

cloudBlob_GetBlobReference.FetchAttributes();

  • 详细说明,GetBlobReference()不进行任何网络调用.它几乎只返回一个用正确的URL初始化的对象.要获取属性,您需要进行网络调用,而.FetchAttributes()是最低限度地执行此操作的方式(执行HEAD请求). (7认同)