I recently wanted to find out the total size of all the files in a particular blob container in my Azure storage account, but to my surprise, this information doesn’t seem to be readily available in either the old or new Azure portal.

Fortunately it can be calculated with only a few lines of code. I tend to use LINQPad for this kind of thing, to create an easily reusable snippet. Let’s look first at my LINQPad script for getting a blob container size, and then we’ll explain what each line does.

var key = Util.GetPassword("Storage Account Key");
var accountName = "mystorageaccount";
var connectionString = "DefaultEndpointsProtocol=https;AccountName=" 
                        + accountName + ";AccountKey=" + key;
var account = CloudStorageAccount.Parse(connectionString);
var blobClient = account.CreateCloudBlobClient();
var blobContainer = blobClient.GetContainerReference("mycontainer");
long fileSize = 0;
foreach (CloudBlockBlob blobItem in blobContainer.ListBlobs())
    fileSize += blobItem.Properties.Length;

So first of all, I need to point out that you will need a reference to the WindowsStorage.Azure NuGet package, and to bring in the Microsoft.WindowsAzure.Storage and Microsoft.WindowsAzure.Storage.Blob namespaces. If you’re using LINQPad, F4 is a handy keyboard shortcut to remember, as it brings up the references dialog.

Then we need to construct a storage account connection string. I’m using a nice helper method in LINQPad called Util.GetPassword that saves me from storing my secret keys in my LINQPad scripts directly. The key can be got from the “Manage Access Keys” link in the Azure portal. You also will need to know your storage account name, and then you can construct your connection string.

From that you create a CloudStorageAccount, and then a CloudBlobClient. This allows you to get a reference to your container, again by supplying its name.

Now all we need to do is call ListBlobs on the blob container, and iterate through adding together the Length. Note that one gotcha is that ListBlobs returns an IEnumerable<IListBlobItem>, and you need to cast these to be CloudBlobClient to access the Length.

And of course, if you are a LINQ enthusiast (which you should be!), you’ll know that you can do this in one line of LINQ like this:

var fileSize = blobContainer.ListBlobs().OfType<CloudBlockBlob>()
                .Sum(b =>b.Properties.Length);

Finally, if you’re not familiar with LINQPad, the .Dump() call at the end is just an extension method that LINQPad provides to print details of whatever object you call it on to the Results panel.

Update: I really ought to have mentioned that if your blobs are in a nested folder structure, then you will need to set the useFlatBlobListing parameter to true to get them all back, like this:

Vote on HN
comments powered by Disqus