This topic describes how to query the storage usage of a specific Object Storage Service (OSS) bucket and the number and storage usage of objects of different storage classes in the bucket.
Usage notes
The sample code in this topic uses the region ID
cn-hangzhou
of the China (Hangzhou) region. By default, the public endpoint is used to access resources in a bucket. If you want to access resources in the bucket by using other Alibaba Cloud services in the same region in which the bucket is located, use the internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.In this topic, access credentials are obtained from environment variables. For more information about how to configure the 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 context of the request, which can be used to specify the total duration of the request. |
request | *GetBucketStatRequest | Specifies the parameters of a specific API operation, such as the bucket name. For more information, see GetBucketStatRequest. |
optFns | ...func(*Options) | Optional. The operation-level parameter. For more information, see Options. |
Response parameters
Response parameter | Type | Description |
result | *GetBucketStatResult | The response to the operation. This parameter is valid when the value of err is nil. For more information, see GetBucketStatResult. |
err | error | The status of the request. If the request fails, the value of err cannot be nil. |
Examples
The following sample code provides an example on how to query the storage usage of a bucket:
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"
)
// Specify the global variables.
var (
region string // The region.
bucketName string // The name of the bucket.
)
// Specify the init function used to initialize command line parameters.
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() {
// Parse command line parameters.
flag.Parse()
// Check whether the bucket name is empty.
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
// Check whether the region is empty.
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
// Load the default configurations and specify the credential provider and region.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
// Create an OSS client.
client := oss.NewClient(cfg)
// Create a request to query the storage usage of the bucket.
request := &oss.GetBucketStatRequest{
Bucket: oss.Ptr(bucketName), // The name of the bucket.
}
// Execute the request to query the storage usage of the bucket and process the result.
result, err := client.GetBucketStat(context.TODO(), request)
if err != nil {
log.Fatalf("failed to get bucket stat %v", err)
}
// Display the storage usage of the bucket.
log.Printf("get bucket stat result:%#v\n", result)
}
Common parameters for bucket storage usage
Parameter | Description |
Storage | The total storage usage 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 are not completed or canceled. |
LiveChannelCount | The number of LiveChannels in the bucket. |
LastModifiedTime | The time when the obtained information is last modified. The value is a UNIX timestamp. Unit: seconds. |
StandardStorage | The storage usage of Standard objects in the bucket. Unit: bytes. |
StandardObjectCount | The number of Standard objects in the bucket. |
InfrequentAccessStorage | The billed storage usage of Infrequent Access (IA) objects in the bucket. Unit: bytes. |
InfrequentAccessRealStorage | The actual storage usage of IA objects in the bucket. Unit: bytes. |
InfrequentAccessObjectCount | The number of IA objects in the bucket. |
ArchiveStorage | The billed storage usage of Archive objects in the bucket. Unit: bytes. |
ArchiveRealStorage | The actual storage usage of Archive objects in the bucket. Unit: bytes. |
ArchiveObjectCount | The number of Archive objects in the bucket. |
ColdArchiveStorage | The billed storage usage of Cold Archive objects in the bucket. Unit: bytes. |
ColdArchiveRealStorage | The actual storage usage of Cold Archive objects in the bucket. Unit: bytes. |
ColdArchiveObjectCount | The number of Cold Archive objects in the bucket. |
References
For the complete sample code that is used to query information about a bucket, visit GitHub.
For more information about the API operation that you can call to query information about a bucket, see GetBucketStat.