![](/img/trans.png)
[英]How to get only folder name in blob using new Azure.Storage.Blobs namespace
[英]How to calculate the total size of Blob storage containers in an storage account when using the new Azure.Storage.Blobs
过去一两年我一直在使用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));
但是,该软件包现已弃用,我已升级为使用Azure.Storage.Blobs
。
我尝试使用此处的 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;
}
}
我也不确定循环遍历它是否是获取存储帐户中容器总大小的正确方法。
有人可以帮忙吗? 谢谢你。
我不知道比循环槽 blob 更好的方法。 我修改了你给出的例子
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;
}
}
而这个用于 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;
}
}
我刚刚使用这些包为 .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>
一切都很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.