Azure存储容器大小

时间:2013-01-17 10:01:10

标签: c# azure azure-storage

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

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

5 个答案:

答案 0 :(得分:7)

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

答案 1 :(得分:6)

可能更完整的方法。关键的区别是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++;
    }                        
}

答案 2 :(得分:5)

我已将 Microsoft.WindowsAzure.StorageClient.dll 1.1.0.0从Windows Azure SDK更新到 Microsoft.WindowsAzure.Storage.dll 2.0.0.0来自 Windows Azure存储 NuGet包,现在可以使用。

long size = 0;
var list = container.ListBlobs();
foreach (CloudBlockBlob blob in list) {
    size += blob.Properties.Length;
}

答案 3 :(得分:1)

定位.NET Core 2, ListBlobs 方法不可用,因为您只能访问异步方法。

因此,您可以使用 ListBlobsSegmentedAsync 方法获取Azure存储容器大小<​​/ p>

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);

答案 4 :(得分:1)

使用Azure.Storage.Blobs版本的12.6.0,可以通过以下方式完成:

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;
    }
}