30
votes

I used Azure blob to store images. Users upload those images. I want to see how much space each user occupy with their images. How can I do that in ASP.NET code behind? Can I get an image file size just by knowing its URI? Or do I need use some blob related function?

2

2 Answers

65
votes

You need to fetch a blob's properties first. Eg,

var blob = _container.GetBlockBlobReference(blobName);
if (blob.Exists())
{
    blob.FetchAttributes();
    return blob.Properties.Length;
}

throw new Exception("Blob doesn't exist");
2
votes

Answering for scala/java here, in case someone is stuck. In order to get the size of the blob you can make use of the following code:

val blob: CloudBlockBlob = container.getBlockBlobReference(blobPath)
blob.downloadAttributes()
blob.getProperties.getLength

Make sure you definitely call downloadAttributes else the properties will be empty.

You can refer to this documentation for more details https://docs.microsoft.com/en-us/java/api/com.microsoft.azure.storage.blob.cloudblockblob?view=azure-java-legacy