Azure Storage container size

Targeting .NET Core 2, ListBlobs method is not available because you can access only async methods.

So you can get Azure Storage Container size using ListBlobsSegmentedAsync method

BlobContinuationToken continuationToken = null;
long totalBytes = 0;
do
{
    var response = await container.ListBlobsSegmentedAsync(continuationToken);
    continuationToken = response.ContinuationToken;
    totalBytes += response.Results.OfType<CloudBlockBlob>().Sum(s => s.Properties.Length);
} while (continuationToken != null);

CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["AzureStoragePrimary"]);
CloudBlobClient blobClient = account.CreateCloudBlobClient();
CloudBlobContainer blobContainer = blobClient.GetContainerReference("myContainer");
int fileSize = 0;
foreach (var blobItem in blobContainer.ListBlobs())
{
    fileSize += blobItem.Properties.Length;
} 

fileSize contains the size of container, i.e. total size of blobs (files) contained.

Reference: CloudBlob: http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storageclient.cloudblob_methods.aspx


I have updated Microsoft.WindowsAzure.StorageClient.dll 1.1.0.0 from Windows Azure SDK to Microsoft.WindowsAzure.Storage.dll 2.0.0.0 from Windows Azure Storage NuGet package and it works now.

long size = 0;
var list = container.ListBlobs();
foreach (CloudBlockBlob blob in list) {
    size += blob.Properties.Length;
}

A potentially more complete approach. The key difference, is the second param in the listblobs() call, which enforces a flat listing:

public class StorageReport
{
    public int FileCount { get; set; }
    public int DirectoryCount { get; set; }
    public long TotalBytes { get; set; }
}

//embdeded in some method
StorageReport report = new StorageReport() { 
    FileCount = 0,
    DirectoryCount = 0,
    TotalBytes = 0
};


foreach (IListBlobItem blobItem in container.ListBlobs(null, true, BlobListingDetails.None))
{
    if (blobItem is CloudBlockBlob)
    {
        CloudBlockBlob blob = blobItem as CloudBlockBlob;
        report.FileCount++;
        report.TotalBytes += blob.Properties.Length;
    }
    else if (blobItem is CloudPageBlob)
    {
        CloudPageBlob pageBlob = blobItem as CloudPageBlob;

        report.FileCount++;
        report.TotalBytes += pageBlob.Properties.Length;
    }
    else if (blobItem is CloudBlobDirectory)
    {
        CloudBlobDirectory directory = blobItem as CloudBlobDirectory;

        report.DirectoryCount++;
    }                        
}