简体   繁体   English

使用新的 Azure.Storage.Blobs 时如何计算存储帐户中 Blob 存储容器的总大小

[英]How to calculate the total size of Blob storage containers in an storage account when using the new Azure.Storage.Blobs

I have been using Microsoft.Azure.Storage.Blob for past year or two and this is how I was calculating the size of all containers:过去一两年我一直在使用Microsoft.Azure.Storage.Blob ,这就是我计算所有容器大小的方式:

    var myStorageAccount = CloudStorageAccount.Parse(myConnectionString, string.Empty));
    var myClient = myStorageAccount.CreateCloudBlobClient();

    var myContainers = myClient.ListContainers();

    containerSize = myContainers .Sum(container => 
      container.ListBlobs(null, true).Cast<CloudBlockBlob>().Sum(blobItem => blobItem.Properties.Length));

However, that package is now deprecated and I have upgraded to use Azure.Storage.Blobs .但是,该软件包现已弃用,我已升级为使用Azure.Storage.Blobs

I tried to use the ListContainers example from here , but it looks like it needs C# 8:我尝试使用此处的 ListContainers 示例,但看起来它需要 C# 8:

async static Task ListContainers(BlobServiceClient blobServiceClient, 
                                string prefix, 
                                int? segmentSize)
{
    string continuationToken = string.Empty;

    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)
                {
                    Console.WriteLine("Container name: {0}", containerItem.Name);
                }

                // Get the continuation token and loop until it is empty.
                continuationToken = containerPage.ContinuationToken;

                Console.WriteLine();
            }

        } while (continuationToken != string.Empty);
    }
    catch (RequestFailedException e)
    {
        Console.WriteLine(e.Message);
        Console.ReadLine();
        throw;
    }
}

I am also not sure if looping through it is the right way to get the total size of the containers in the storage account.我也不确定循环遍历它是否是获取存储帐户中容器总大小的正确方法。

Can someone please help?有人可以帮忙吗? Thank you.谢谢你。

I don't know a better way then looping trough blobs.我不知道比循环槽 blob 更好的方法。 I modified given by you example我修改了你给出的例子

wchich works in C# 8.0 wchich 在 C# 8.0 中工作

async static Task<Dictionary<string, long>> ListContainers(BlobServiceClient blobServiceClient,
                                        string connectionString,
                                        string prefix,
                                        int? segmentSize)
{
    string continuationToken = string.Empty;
    var sizes = new Dictionary<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 size = container.GetBlobs().Sum(b => b.Properties.ContentLength.GetValueOrDefault());

                    sizes.Add(containerItem.Name, size);

                    Console.WriteLine("Container name: {0} size: {1}", containerItem.Name.PadRight(30), size);
                }

                // Get the continuation token and loop until it is empty.
                continuationToken = containerPage.ContinuationToken;

                Console.WriteLine();
            }

        } while (continuationToken != string.Empty);

        return sizes;
    }
    catch (RequestFailedException e)
    {
        Console.WriteLine(e.Message);
        Console.ReadLine();
        throw;
    }
}

and this one for C# 7.x (to compile you just need the ToListAsync() method, which is in the System.Linq.Async NuGet package. )而这个用于 C# 7.x(编译你只需要ToListAsync()方法,它在System.Linq.Async NuGet 包中。)

async static Task<Dictionary<string, long>> ListContainers(BlobServiceClient blobServiceClient,
                                        string connectionString,
                                        string prefix,
                                        int? segmentSize)
        {
            string continuationToken = string.Empty;
            var sizes = new Dictionary<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 =
                        await blobServiceClient.GetBlobContainersAsync(BlobContainerTraits.Metadata, prefix, default)
                        .AsPages(continuationToken, segmentSize).ToListAsync();
                     foreach (Azure.Page<BlobContainerItem> containerPage in resultSegment)
                    {

                        foreach (BlobContainerItem containerItem in containerPage.Values)
                        {
                            BlobContainerClient container = new BlobContainerClient(connectionString, containerItem.Name);

                            var size = container.GetBlobs().Sum(b => b.Properties.ContentLength.GetValueOrDefault());

                            sizes.Add(containerItem.Name, size);

                            Console.WriteLine("Container name: {0} size: {1}", containerItem.Name.PadRight(30), size);
                        }

                        // Get the continuation token and loop until it is empty.
                        continuationToken = containerPage.ContinuationToken;

                        Console.WriteLine();
                    }

                } while (continuationToken != string.Empty);

                return sizes;
            }
            catch (RequestFailedException e)
            {
                Console.WriteLine(e.Message);
                Console.ReadLine();
                throw;
            }
        }

I just ran this code for .NET 4.8 using these packages:我刚刚使用这些包为 .NET 4.8 运行了这段代码:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Azure.Core" version="1.4.1" targetFramework="net48" />
  <package id="Azure.Storage.Blobs" version="12.6.0" targetFramework="net48" />
  <package id="Azure.Storage.Common" version="12.5.2" targetFramework="net48" />
  <package id="Microsoft.Bcl.AsyncInterfaces" version="1.1.0" targetFramework="net48" />
  <package id="System.Buffers" version="4.5.0" targetFramework="net48" />
  <package id="System.Diagnostics.DiagnosticSource" version="4.6.0" targetFramework="net48" />
  <package id="System.Linq.Async" version="4.1.1" targetFramework="net48" />
  <package id="System.Memory" version="4.5.3" targetFramework="net48" />
  <package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net48" />
  <package id="System.Runtime.CompilerServices.Unsafe" version="4.6.0" targetFramework="net48" />
  <package id="System.Text.Encodings.Web" version="4.6.0" targetFramework="net48" />
  <package id="System.Text.Json" version="4.6.0" targetFramework="net48" />
  <package id="System.Threading.Tasks.Extensions" version="4.5.2" targetFramework="net48" />
  <package id="System.ValueTuple" version="4.5.0" targetFramework="net48" />
</packages>

and all was fine.一切都很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用新的 Azure.Storage.Blobs 命名空间仅获取 blob 中的文件夹名称 - How to get only folder name in blob using new Azure.Storage.Blobs namespace 如何使用 Azure.Storage.Blobs 程序集对 Azure blob 存储操作设置重试策略? - How do I set a retry policy on an Azure blob storage operation using the Azure.Storage.Blobs assembly? 如何使用 C# 中的 Azure.Storage.Blobs 以 ByteArray 格式从 Azure 存储 blob 中获取文件 - How to get file from Azure storage blob in a ByteArray format using Azure.Storage.Blobs in C# 如何使用 Azure.Storage.Blobs BlobClient 检索 blob 目录路径中的 blob? - How to retrieve blobs within a blob directory path using the Azure.Storage.Blobs BlobClient? 想要使用新的 sdk - Azure.Storage.Blobs package 从 blob 内的文件夹下载和上传文件 - Want to download and upload file from a folder inside the blob using new sdk - Azure.Storage.Blobs package 如何模拟 Azure.Storage.Blobs 的 BlobContainerClient()? - How to Mock BlobContainerClient() of Azure.Storage.Blobs? 如何使用 Azure.Storage.Blobs 上传 stream - How to upload stream with Azure.Storage.Blobs 无法使用 Azure.Storage.Blobs NuGet 包将文件上传到 Azure Blob 存储 - Unable to upload a file to Azure Blob Storage using Azure.Storage.Blobs NuGet package 有没有办法在 Azure.Storage.Blobs 中获取 blob 的 ContentType? - Is there a way to get a blob's ContentType in Azure.Storage.Blobs? Azure.Storage.Blobs:找不到有效的帐户信息组合 - Azure.Storage.Blobs: No valid combination of account information found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM