Tue, 01 Jun 2010

Computing the Total Size of Your Blobs

A question came up on the Windows Azure MSDN forum recently about how to find the total number of bytes used by a blobs in a particular container. There’s no API that retrieves that information at the container level, but you can compute it by enumerating the blobs, as in the following one-liner:

var totalBytes = (from CloudBlob blob in
                  container.ListBlobs(new BlobRequestOptions() { UseFlatBlobListing = true })
                  select blob.Properties.Length
                 ).Sum();

To go one step further, we can enumerate all the containers too. Here’s how to sum the sizes of all the blobs in a particular account (still a one-liner):

var totalBytes = (from container in blobClient.ListContainers()
                  select
                  (from CloudBlob blob in
                   container.ListBlobs(new BlobRequestOptions() { UseFlatBlobListing = true })
                   select blob.Properties.Length
                  ).Sum()
                 ).Sum();

Note that this does not reflect the number of bytes you’re billed for. Things like empty pages in page blobs, uncommitted blocks in block blobs, snapshots, metadata, etc. all affect the total storage used in your account. The code snippets above simply sums the “sizes” (if you were to download them, for example) of all the blobs.