Windows Azure Blob size return 0 even when blob is accessible and downloaded without any problem

Following is the code which shows Windows Azure Blob size as “0” even when the same blob is accessible and could be downloaded without any problem:

  StorageCredentialsAccountAndKey creds = new StorageCredentialsAccountAndKey("AZURE_STORAGE_NAME", "AZURE_STORAGE_KEY”);
 string baseUri = string.Format("https://{0}.blob.core.windows.net", " AZURE_STORAGE_NAME");
 CloudBlobClient blobStorage = new CloudBlobClient(baseUri, creds);
 CloudBlobContainer container = blobStorage.GetContainerReference("AZURE_STORAGE_CONTAINER_NAME");
 var blob = container.GetBlobReference("YOUR_BLOB_NAME ");
 Console.WriteLine("Blob Size: " + blob.Properties.Length);
 
 

The above does works fine and can download the blob however the blob length property will return “0”. This is because the blob length property is part of blob metadata and you would need to download the blob attribute to get blob meta data only then you will have blob length available.

The updated code is as below:

 
  StorageCredentialsAccountAndKey creds = new StorageCredentialsAccountAndKey("AZURE_STORAGE_NAME", "AZURE_STORAGE_KEY”);
 string baseUri = string.Format("https://{0}.blob.core.windows.net", " AZURE_STORAGE_NAME");
 CloudBlobClient blobStorage = new CloudBlobClient(baseUri, creds);
 CloudBlobContainer container = blobStorage.GetContainerReference("AZURE_STORAGE_CONTAINER_NAME");
 var blob = container.GetBlobReference("YOUR_BLOB_NAME ");
 blob.FetchAttributes();
 Console.WriteLine("Blob Size: " + blob.Properties.Length);
 

So adding "blob.FetchAttributes();" does resolve this issue.