6

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.

4

2 回答 2

14

看起来您缺少对 FetchAttributes 方法的调用,该方法加载 blob 的元数据:

blob.FetchAttributes(); 

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

于 2012-04-26T11:41:51.933 回答
0

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

await blobContainer.GetBlobReferenceFromServerAsync(blobPath);
于 2019-10-04T01:24:35.407 回答