How to get size of Azure CloudBlobContainer

喜你入骨 提交于 2019-12-21 13:29:06

问题


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`?


回答1:


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;
}



回答2:


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);


来源:https://stackoverflow.com/questions/14901965/how-to-get-size-of-azure-cloudblobcontainer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!