我该如何获取Azure存储容器的大小?我通过C# API访问Azure存储:
var account = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["AzureStoragePrimary"]);
var client = account.CreateCloudBlobClient();
var container = client.GetContainerReference("myContainer");
我该如何获取Azure存储容器的大小?我通过C# API访问Azure存储:
var account = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["AzureStoragePrimary"]);
var client = account.CreateCloudBlobClient();
var container = client.GetContainerReference("myContainer");
一种可能更完整的方法。关键区别在于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++;
}
}
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
long size = 0; foreach (var blob in container.ListBlobs()) { size += container.GetBlobReference(blob.Uri.AbsoluteUri).Properties.Length; }
总是返回0。 - Václav Dajbych我已经将Windows Azure SDK中的Microsoft.WindowsAzure.StorageClient.dll 1.1.0.0更新为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;
}
针对 .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);
使用版本为12.6.0
的Azure.Storage.Blobs
,可以按照以下方式完成:
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;
}
}
async Task Main
并将 .Wait()
替换为 await
。 - Florin-Constantin Ciubotariu
var account
? - Raptor