Azure CloubdBlob的Properties.Length返回0 [英] Azure CloubdBlob's Properties.Length returns 0
问题描述
以下代码返回blob文件大小为0:
The following code returns blob file size of 0:
public long GetFileSize(string fileUrl)
{
var blob = GetBlobContainer().GetBlobReference(fileUrl);
return blob == null ? 0 : blob.Properties.Length;
}
,几乎找不到它.但是,我删除了Blob之后,我发现它已被删除.删除时有效:
, its almost as it does not find the blob. But of I delete the blob I see that it gets deleted. This works when deleting:
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.
Its basically the same code as the one above, so it seems that the blob is found.
如果我遍历blob,我将获得正确的blob文件大小,就像在计算存储的总字节数时所做的那样:
If I iterate through the blobs I get the correct blob file size, like I do when I calculate the total amount of stored bytes i my storage:
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的原因.
So, I cant figure out why the CloubdBlob::Properties.Length returns 0 when I use GetBlobReference with a url.
推荐答案
您似乎缺少对FetchAttributes方法的调用,该方法加载了Blob的元数据:
It looks like you're missing a call to the FetchAttributes method, which loads the blob's metadata:
blob.FetchAttributes();
参考: https://azure.microsoft.com/en-us/documentation/articles/storage-properties-metadata/
这篇关于Azure CloubdBlob的Properties.Length返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!