Azure CloubdBlob的Properties.Length返回0

4 azure azure-storage azure-table-storage

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

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

,它几乎没有找到blob.但是我删除了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();
}
Run Code Online (Sandbox Code Playgroud)

它与上面的代码基本相同,所以似乎找到了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();            
}
Run Code Online (Sandbox Code Playgroud)

所以,当我使用带有url的GetBlobReference时,我无法弄清楚为什么CloubdBlob :: Properties.Length返回0.

San*_*tia 10

看起来你错过了对FetchAttributes方法的调用,该方法加载了blob的元数据:

blob.FetchAttributes(); 
Run Code Online (Sandbox Code Playgroud)

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

  • 现在是 Azure Java SDK 2.0.0 版中的 blob.downloadAttributes():http://javadox.com/com.microsoft.azure/azure-storage/2.0.0/com/microsoft/azure/storage/blob /CloudBlob.html#downloadAttributes() (2认同)