如何在 Azure 存储中获取容器的大小?I 通过 C# API 访问 Azure 存储:
var account = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["AzureStoragePrimary"]);
var client = account.CreateCloudBlobClient();
var container = client.GetContainerReference("myContainer");
我已将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;
}
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
一种可能更完整的方法。关键区别在于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++;
}
}