This topic describes how to query the storage capacity of a specified bucket, including the number of objects and storage capacity for each storage class.
Notes
The sample code in this topic uses the China (Hangzhou) region (cn-hangzhou) as an example. By default, the public endpoint is used. If you want to access OSS from other Alibaba Cloud services in the same region, use the internal endpoint. For more information about the regions and endpoints that OSS supports, see Regions and endpoints.
Sample code
You can use the following code to query the storage capacity of a bucket.
using OSS = AlibabaCloud.OSS.V2; // Create an alias for Alibaba Cloud OSS SDK to simplify subsequent use.
var region = "cn-hangzhou"; // Required. Specify the region where the bucket is located. In this example, the China (Hangzhou) region is used. Set the region to cn-hangzhou.
var bucket = "your bucket name"; // Required. Specify the name of the destination bucket.
var endpoint = null as string; // Optional. Specify the endpoint used to access the OSS service. In this example, the China (Hangzhou) region is used. Set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
// Load the default configurations of the OSS SDK. The configurations automatically read credential information (such as AccessKey) from environment variables.
var cfg = OSS.Configuration.LoadDefault();
// Explicitly set to use environment variables to obtain credentials for identity verification (format: OSS_ACCESS_KEY_ID, OSS_ACCESS_KEY_SECRET).
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
// Set the region of the bucket in the configuration.
cfg.Region = region;
// If an endpoint is specified, overwrite the default endpoint.
if(endpoint != null)
{
cfg.Endpoint = endpoint;
}
// Create an OSS client instance using the configuration information.
using var client = new OSS.Client(cfg);
// Call the GetBucketStatAsync method to query the storage capacity information of the specified bucket.
var result = await client.GetBucketStatAsync(new OSS.Models.GetBucketStatRequest()
{
Bucket = bucket
});
// Print the result information.
Console.WriteLine("GetBucketStat done"); // The operation is complete.
Console.WriteLine($"StatusCode: {result.StatusCode}"); // The HTTP status code.
Console.WriteLine($"RequestId: {result.RequestId}"); // The request ID, which is used by Alibaba Cloud to troubleshoot issues.
Console.WriteLine("Response Headers:"); // The response header information.
result.Headers.ToList().ForEach(x => Console.WriteLine(x.Key + " : " + x.Value)); // Traverse and print all response headers.
Console.WriteLine($"Storage: {result.BucketStat!.Storage}"); // Print the total storage capacity of the bucket.
Console.WriteLine($"ObjectCount: {result.BucketStat!.ObjectCount}"); // Print the total number of objects in the bucket.Common storage capacity information
Parameter | Description |
Storage | The total storage capacity of the bucket. Unit: bytes. |
ObjectCount | The total number of objects in the bucket. |
MultipartUploadCount | The number of multipart upload tasks that have been initiated but not completed or aborted in the bucket. |
LiveChannelCount | The number of LiveChannels in the bucket. |
LastModifiedTime | The point in time when the storage information was obtained by this call. The value is a UNIX timestamp. Unit: seconds. |
StandardStorage | The storage capacity of Standard objects. Unit: bytes. |
StandardObjectCount | The number of Standard objects. |
InfrequentAccessStorage | The billable storage capacity of Infrequent Access (IA) objects. Unit: bytes. |
InfrequentAccessRealStorage | The actual storage capacity of IA objects. Unit: bytes. |
InfrequentAccessObjectCount | The number of IA objects. |
ArchiveStorage | The billable storage capacity of Archive Storage objects. Unit: bytes. |
ArchiveRealStorage | The actual storage capacity of Archive Storage objects. Unit: bytes. |
ArchiveObjectCount | The number of Archive Storage objects. |
ColdArchiveStorage | The billable storage capacity of Cold Archive objects. Unit: bytes. |
ColdArchiveRealStorage | The actual storage capacity of Cold Archive objects. Unit: bytes. |
ColdArchiveObjectCount | The number of Cold Archive objects. |
References
For the complete sample code, see GetBucketStat.cs.