14

如何在 Azure 存储中获取容器的大小?我通过 C# API 访问 Azure 存储:

var account = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["AzureStoragePrimary"]);
var client = account.CreateCloudBlobClient();
var container = client.GetContainerReference("myContainer");
4

5 回答 5

9

一种可能更完整的方法。关键区别在于 listblobs() 调用中的第二个参数,它强制执行平面列表:

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++;
    }                        
}
于 2016-03-09T16:16:09.370 回答
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包含容器的大小,即包含的 blob(文件)的总大小。

参考:CloudBlob:http: //msdn.microsoft.com/en-us/library/microsoft.windowsazure.storageclient.cloudblob_methods.aspx

于 2013-01-17T10:22:04.137 回答
6

我已将Microsoft.WindowsAzure.StorageClient.dll 1.1.0.0 从 Windows Azure SDK 更新为来自Windows Azure Storage NuGet 包的Microsoft.WindowsAzure.Storage.dll 2.0.0.0 ,它现在可以工作了。

long size = 0;
var list = container.ListBlobs();
foreach (CloudBlockBlob blob in list) {
    size += blob.Properties.Length;
}
于 2013-01-31T22:04:38.060 回答
2

针对 .NET Core 2,ListBlobs方法不可用,因为您只能访问异步方法。

因此,您可以使用ListBlobsSegmentedAsync方法获取 Azure 存储容器大小

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);
于 2018-04-30T07:59:53.717 回答
2

Azure.Storage.Blobs版本中12.6.0,可以通过以下方式完成:

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;
    }
}
于 2020-11-02T02:54:02.017 回答