当前位置: 首页 > 知识库问答 >
问题:

Azure Storage container size

欧博简
2023-03-14

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

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

共有3个答案

柴凌
2023-03-14

我已将Microsoft.WindowsAzure.StorageClient.dll1.1.0.0从Windows Azure SDK更新为从Windows Azure存储NuGet包的Microsoft.WindowsAzure.Storage.dll2.0.0.0,它现在可以工作了。

long size = 0;
var list = container.ListBlobs();
foreach (CloudBlockBlob blob in list) {
    size += blob.Properties.Length;
}
闻人梓
2023-03-14
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;
} 

文件大小包含容器的大小,即包含的blob(文件)的总大小。

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

山高峰
2023-03-14

一种可能更完整的方法。关键区别在于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++;
    }                        
}
 类似资料:

相关问答

相关文章

相关阅读