All Products
Search
Document Center

Object Storage Service:Query the storage capacity of a bucket (C# SDK V2)

Last Updated:Mar 20, 2026

Query a bucket's storage statistics — total size, object counts, and per-storage-class breakdowns — using a single GetBucketStatAsync call.

Prerequisites

Before you begin, ensure that you have:

  • An OSS bucket

  • OSS SDK for C# V2 installed in your project

Query bucket storage statistics

The following example queries storage statistics for a bucket in the China (Hangzhou) region (cn-hangzhou). The code uses the public endpoint by default. To access OSS from another Alibaba Cloud service in the same region, set endpoint to the internal endpoint. For supported regions and endpoints, see Regions and endpoints.

using OSS = AlibabaCloud.OSS.V2;

var region = "cn-hangzhou";       // Region where the bucket is located
var bucket = "examplebucket";     // Name of the target bucket
var endpoint = null as string;    // Optional. Set to the internal endpoint for same-region VPC access

// Load default SDK configuration. Reads credentials from environment variables:
// OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET
var cfg = OSS.Configuration.LoadDefault();
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
cfg.Region = region;
if (endpoint != null)
{
    cfg.Endpoint = endpoint;
}

using var client = new OSS.Client(cfg);

try
{
    var result = await client.GetBucketStatAsync(new OSS.Models.GetBucketStatRequest()
    {
        Bucket = bucket
    });

    Console.WriteLine("GetBucketStat done");
    Console.WriteLine($"StatusCode: {result.StatusCode}");
    Console.WriteLine($"RequestId: {result.RequestId}");  // Use this ID when contacting Alibaba Cloud support
    Console.WriteLine("Response headers:");
    result.Headers.ToList().ForEach(x => Console.WriteLine($"  {x.Key}: {x.Value}"));
    Console.WriteLine($"Storage: {result.BucketStat!.Storage}");        // Total storage, in bytes
    Console.WriteLine($"ObjectCount: {result.BucketStat!.ObjectCount}"); // Total object count
}
catch (Exception ex)
{
    Console.WriteLine($"Request failed: {ex.Message}");
}
Credentials are read from the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables. Avoid hardcoding AccessKey credentials in source code.

Response fields

GetBucketStatAsync returns a BucketStat object with the following fields.

FieldDescription
StorageTotal storage capacity of the bucket, in bytes.
ObjectCountTotal number of objects in the bucket.
MultipartUploadCountNumber of multipart upload tasks initiated but not yet completed or aborted.
LiveChannelCountNumber of LiveChannels in the bucket.
LastModifiedTimeUnix timestamp (seconds) indicating when this storage snapshot was captured.
StandardStorageStorage capacity of Standard objects, in bytes.
StandardObjectCountNumber of Standard objects.
InfrequentAccessStorageBillable storage capacity of Infrequent Access (IA) objects, in bytes.
InfrequentAccessRealStorageActual storage capacity of IA objects, in bytes.
InfrequentAccessObjectCountNumber of IA objects.
ArchiveStorageBillable storage capacity of Archive Storage objects, in bytes.
ArchiveRealStorageActual storage capacity of Archive Storage objects, in bytes.
ArchiveObjectCountNumber of Archive Storage objects.
ColdArchiveStorageBillable storage capacity of Cold Archive objects, in bytes.
ColdArchiveRealStorageActual storage capacity of Cold Archive objects, in bytes.
ColdArchiveObjectCountNumber of Cold Archive objects.
InfrequentAccessStorage, ArchiveStorage, and ColdArchiveStorage reflect the billable size, which may differ from the actual size due to minimum storage duration and size requirements. Use the corresponding *RealStorage fields to see the actual stored size.

What's next