Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get size of Azure CloudBlobContainer

I'm creating a .net wrapper service for my application that utilizes Azure Blob Storage as a file store. My application creates a new CloudBlobContainer for each "account" on my system. Each account is limited to a maximum amount of storage.

What is the simplest and most efficient way to query the current size (space utilization) of an Azure CloudBlobContainer`?

like image 708
kmehta Avatar asked Feb 15 '13 19:02

kmehta


People also ask

How do I find my Azure container size?

Azure portal : Open the blob container, select properties under Settings, then click on the Calculate Size button which gives size for each type of blob and also total size of the container.

How do I check my blob size?

Azure Blob Sizes And on the right you can select the BLOB view tab to see the blobs in that container and of course the size of each blob. The Blob view is sorted based on size, so if even if you have hundreds or thousands of blobs, you will see which ones are taking up the most space first.

What is the size of Azure Blob storage?

Scale targets for Blob storage Specifically, call the Put Blob or Put Block operation with a blob or block size that is greater than 4 MiB for standard storage accounts. For premium block blob or for Data Lake Storage Gen2 storage accounts, use a block or blob size that is greater than 256 KiB.


2 Answers

FYI here's the answer. Hope this helps.

public static long GetSpaceUsed(string containerName)
{
    var container = CloudStorageAccount
        .Parse(ConfigurationManager.ConnectionStrings["StorageConnection"].ConnectionString)
        .CreateCloudBlobClient()
        .GetContainerReference(containerName);
    if (container.Exists())
    {
        return (from CloudBlockBlob blob in
                container.ListBlobs(useFlatBlobListing: true)
                select blob.Properties.Length
               ).Sum();
    }
    return 0;
}
like image 129
kmehta Avatar answered Oct 27 '22 05:10

kmehta


As of version v9.x.x.x or greater of WindwosAzure.Storage.dll (from Nuget package), ListBlobs method is no longer available publicly. So the solution for applications targeting .NET Core 2.x+ would be like following:

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);
like image 21
Azaz ul Haq Avatar answered Oct 27 '22 04:10

Azaz ul Haq