Azure CloubdBlob's Properties.Length returns 0

怎甘沉沦 提交于 2019-12-07 08:17:16

问题


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;
}

, 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();
}

Its basically the same code as the one above, so it seems that the blob is found.

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();            
}

So, I cant figure out why the CloubdBlob::Properties.Length returns 0 when I use GetBlobReference with a url.


回答1:


It looks like you're missing a call to the FetchAttributes method, which loads the blob's metadata:

blob.FetchAttributes(); 

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




回答2:


Getting Ref from the server should do the trick!

await blobContainer.GetBlobReferenceFromServerAsync(blobPath);


来源:https://stackoverflow.com/questions/10332295/azure-cloubdblobs-properties-length-returns-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!