Azure Storage container size
Solution 1
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;
}
Solution 2
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++;
}
}
Solution 3
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
Solution 4
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;
}
}
Solution 5
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);
Related videos on Youtube
Comments
-
Václav Dajbych about 1 year
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");
-
Václav Dajbych almost 11 yearsThis
long size = 0; foreach (var blob in container.ListBlobs()) { size += container.GetBlobReference(blob.Uri.AbsoluteUri).Properties.Length; }
returns always 0. -
Rod almost 8 yearswhats value return ? kb? mb?
-
Elliot Wood over 6 yearsThis is great! It did the trick on over 6TB of data and millions of files
-
pim over 6 years@ElliotWood that's wild! Glad it stood up.
-
Václav Dajbych about 3 yearsA long value containing the container's size in bytes.
-
Florin-Constantin Ciubotariu almost 3 yearsThanks for the snippet. On C# >= 7.1, you can use
async Task Main
and replace.Wait()
withawait