How to get size of Azure CloudBlobContainer
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;
}
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);