Search code examples
azureazure-storageazure-table-storage

Azure CloubdBlob's Properties.Length returns 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;
}

, 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.


Solution

  • 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/