Call GetBucketStat to retrieve a bucket's total storage usage and object count, broken down by storage class.
Usage notes
The sample code uses the region ID
cn-hangzhoufor the China (Hangzhou) region. By default, a public endpoint is used to access resources in a bucket. To access bucket resources from other Alibaba Cloud services in the same region, use the internal endpoint. For more information, see Regions and endpoints.Access credentials in this topic are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.
Method
func (c *Client) GetBucketStat(ctx context.Context, request *GetBucketStatRequest, optFns ...func(*Options)) (*GetBucketStatResult, error)Request parameters
| Parameter | Type | Description |
|---|---|---|
ctx | context.Context | The request context. Use this to set a timeout or deadline for the request. |
request | *GetBucketStatRequest | The request parameters, including the bucket name. For details, see GetBucketStatRequest. |
optFns | ...func(*Options) | Optional operation-level options. For details, see Options. |
Response parameters
| Parameter | Type | Description |
|---|---|---|
result | *GetBucketStatResult | The response. Valid when err is nil. For details, see GetBucketStatResult. |
err | error | The request status. Non-nil if the request fails. |
Example
package main
import (
"context"
"flag"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
var (
region string
bucketName string
)
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}
func main() {
flag.Parse()
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
// Load the default configuration with credentials from environment variables.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
// Create an OSS client.
client := oss.NewClient(cfg)
// Build the request.
request := &oss.GetBucketStatRequest{
Bucket: oss.Ptr(bucketName),
}
// Send the request.
result, err := client.GetBucketStat(context.TODO(), request)
if err != nil {
log.Fatalf("GetBucketStat: %v", err)
}
log.Printf("GetBucketStat result: %#v\n", result)
}Response fields
GetBucketStatResult contains the following fields.
General statistics
| Field | Description |
|---|---|
Storage | Total storage usage of the bucket, in bytes. Includes all storage classes. |
ObjectCount | Total number of objects in the bucket, across all storage classes. |
MultipartUploadCount | Number of multipart upload tasks that have been initiated but are not yet completed or canceled. |
LiveChannelCount | Number of LiveChannels in the bucket. |
LastModifiedTime | UNIX timestamp (in seconds) of when the statistics were last updated. |
Per-storage-class breakdown
Each storage class exposes two storage fields and one object count field.
| Field | Description |
|---|---|
StandardStorage | Storage usage of Standard objects, in bytes. |
StandardObjectCount | Number of Standard objects. |
InfrequentAccessStorage | Billed storage usage of Infrequent Access (IA) objects, in bytes. |
InfrequentAccessRealStorage | Actual storage usage of IA objects, in bytes. |
InfrequentAccessObjectCount | Number of IA objects. |
ArchiveStorage | Billed storage usage of Archive objects, in bytes. |
ArchiveRealStorage | Actual storage usage of Archive objects, in bytes. |
ArchiveObjectCount | Number of Archive objects. |
ColdArchiveStorage | Billed storage usage of Cold Archive objects, in bytes. |
ColdArchiveRealStorage | Actual storage usage of Cold Archive objects, in bytes. |
ColdArchiveObjectCount | Number of Cold Archive objects. |
What's next
For the complete sample, see get_bucket_stat.go on GitHub.
For the full API reference, see GetBucketStat.