Azure Storage container size
Asked Answered
L

5

14

How can I get a size of container in Azure Storage? I access Azure storage via C# API:

var account = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["AzureStoragePrimary"]);
var client = account.CreateCloudBlobClient();
var container = client.GetContainerReference("myContainer");
Lentha answered 17/1, 2013 at 10:1 Comment(1)
are you sure you are using C# ? var account ?Gavette
L
6

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;
}
Lentha answered 31/1, 2013 at 22:4 Comment(2)
whats value return ? kb? mb?Matabele
A long value containing the container's size in bytes.Lentha
C
9

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++;
    }                        
}
Commiserate answered 9/3, 2016 at 16:16 Comment(2)
This is great! It did the trick on over 6TB of data and millions of filesMozell
@ElliotWood that's wild! Glad it stood up.Commiserate
G
8
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

Gavette answered 17/1, 2013 at 10:22 Comment(1)
This long size = 0; foreach (var blob in container.ListBlobs()) { size += container.GetBlobReference(blob.Uri.AbsoluteUri).Properties.Length; } returns always 0.Lentha
L
6

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;
}
Lentha answered 31/1, 2013 at 22:4 Comment(2)
whats value return ? kb? mb?Matabele
A long value containing the container's size in bytes.Lentha
T
3

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);
Tami answered 30/4, 2018 at 7:59 Comment(0)
L
2

With Azure.Storage.Blobs in version 12.6.0 it can be done in this way:

static void Main(string[] args)
{

    BlobServiceClient client = new BlobServiceClient(connectionString);

    GetContainersSize(client, connectionString, null, null).Wait();
}

//-------------------------------------------------
// List containers
//-------------------------------------------------
async static Task<ConcurrentDictionary<string, long>> GetContainersSize(BlobServiceClient blobServiceClient,
                                string connectionString,
                                string prefix,
                                int? segmentSize)
{
    string continuationToken = string.Empty;
    var sizes = new ConcurrentDictionary<string, long>();
    try
    {

        do
        {
            // Call the listing operation and enumerate the result segment.
            // When the continuation token is empty, the last segment has been returned
            // and execution can exit the loop.
            var resultSegment =
                blobServiceClient.GetBlobContainersAsync(BlobContainerTraits.Metadata, prefix, default)
                .AsPages(continuationToken, segmentSize);
            await foreach (Azure.Page<BlobContainerItem> containerPage in resultSegment)
            {

                foreach (BlobContainerItem containerItem in containerPage.Values)
                {
                    BlobContainerClient container = new BlobContainerClient(connectionString, containerItem.Name);

                    var blobs = container.GetBlobsAsync().AsPages(continuationToken);

                    await foreach(var blobPage in blobs)
                    {
                        var blobPageSize = blobPage.Values.Sum(b => b.Properties.ContentLength.GetValueOrDefault());
                        sizes.AddOrUpdate(containerItem.Name, blobPageSize, (key, currentSize) => currentSize + blobPageSize);
                    }
                }

                // Get the continuation token and loop until it is empty.
                continuationToken = containerPage.ContinuationToken;
            }

        } while (continuationToken != string.Empty);

        return sizes;
    }
    catch (RequestFailedException e)
    {
        Console.WriteLine(e.Message);
        Console.ReadLine();
        throw;
    }
}
Licketysplit answered 2/11, 2020 at 2:54 Comment(1)
Thanks for the snippet. On C# >= 7.1, you can use async Task Main and replace .Wait() with awaitHusain

© 2022 - 2024 — McMap. All rights reserved.