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 theOSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETenvironment variables. Avoid hardcoding AccessKey credentials in source code.
Response fields
GetBucketStatAsync returns a BucketStat object with the following fields.
| Field | Description |
|---|---|
Storage | Total storage capacity of the bucket, in bytes. |
ObjectCount | Total number of objects in the bucket. |
MultipartUploadCount | Number of multipart upload tasks initiated but not yet completed or aborted. |
LiveChannelCount | Number of LiveChannels in the bucket. |
LastModifiedTime | Unix timestamp (seconds) indicating when this storage snapshot was captured. |
StandardStorage | Storage capacity of Standard objects, in bytes. |
StandardObjectCount | Number of Standard objects. |
InfrequentAccessStorage | Billable storage capacity of Infrequent Access (IA) objects, in bytes. |
InfrequentAccessRealStorage | Actual storage capacity of IA objects, in bytes. |
InfrequentAccessObjectCount | Number of IA objects. |
ArchiveStorage | Billable storage capacity of Archive Storage objects, in bytes. |
ArchiveRealStorage | Actual storage capacity of Archive Storage objects, in bytes. |
ArchiveObjectCount | Number of Archive Storage objects. |
ColdArchiveStorage | Billable storage capacity of Cold Archive objects, in bytes. |
ColdArchiveRealStorage | Actual storage capacity of Cold Archive objects, in bytes. |
ColdArchiveObjectCount | Number of Cold Archive objects. |
InfrequentAccessStorage,ArchiveStorage, andColdArchiveStoragereflect the billable size, which may differ from the actual size due to minimum storage duration and size requirements. Use the corresponding*RealStoragefields to see the actual stored size.
What's next
For the complete runnable sample, see GetBucketStat.cs.
For region IDs and endpoint formats, see Regions and endpoints.