Azure CloubdBlob的属性。长度返回0

问题描述 投票:4回答:2

以下代码返回的blob文件大小为0:

public long GetFileSize(string fileUrl)
{
    var blob = GetBlobContainer().GetBlobReference(fileUrl);
    return blob == null ? 0 : blob.Properties.Length;
}

,几乎找不到斑点。但是,我删除了Blob后,看到它被删除了。删除时有效:

void DeleteFileFromBlob(string fileUrl, bool deleteSnapshots)
{
    var blob = GetBlobContainer().GetBlobReference(fileUrl);
    if (deleteSnapshots)
    {
        var options = new BlobRequestOptions { DeleteSnapshotsOption = DeleteSnapshotsOption.IncludeSnapshots };
        blob.DeleteIfExists(options);
    }
    else blob.DeleteIfExists();
}

与上面的代码基本相同,因此似乎找到了blob。

如果我遍历blob,我将获得正确的blob文件大小,就像我计算存储的总字节数时所做的那样:

public long GetStorageUsageByteSize()
{
    var blobClient = GetBlobClient();
    return (from container in blobClient.ListContainers()
                      select
                      (from CloudBlob blob in
                           container.ListBlobs(new BlobRequestOptions { UseFlatBlobListing = true })
                       select blob.Properties.Length
                      ).Sum()
                     ).Sum();            
}

因此,当我将GetBlobReference与URL一起使用时,我无法弄清楚CloubdBlob :: Properties.Length为什么返回0。

azure azure-storage azure-table-storage
2个回答
10
投票

似乎您缺少对FetchAttributes方法的调用,该方法将加载Blob的元数据:

blob.FetchAttributes(); 

参考:https://azure.microsoft.com/en-us/documentation/articles/storage-properties-metadata/


0
投票

从服务器获取引用应该可以解决问题!

await blobContainer.GetBlobReferenceFromServerAsync(blobPath);
© www.soinside.com 2019 - 2024. All rights reserved.