Use GetBucketStat to retrieve the total storage usage of a bucket and the object count and storage breakdown by storage class.
Prerequisites
Before you begin, make sure that you have:
Go SDK 2.2.5 or later installed
Access credentials configured as environment variables. See Configure access credentials.
An OSSClient instance. See Configure OSSClient instances.
Usage notes
The sample code uses the public endpoint for the China (Hangzhou) region. If you access OSS from another Alibaba Cloud service in the same region, use the internal endpoint instead. See Regions and endpoints.
To create an OSSClient instance using a custom domain name or Security Token Service (STS), see Configure OSSClient instances.
Query bucket storage capacity
The following example queries the storage usage of examplebucket.
package main
import (
"log"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Obtain access credentials from environment variables.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
log.Fatalf("Failed to get credentials from environment variables: %v", err)
}
// Create an OSSClient instance.
// Replace yourEndpoint with the bucket endpoint.
// China (Hangzhou): https://oss-cn-hangzhou.aliyuncs.com
// Replace yourRegion with the region where the bucket is located.
// China (Hangzhou): cn-hangzhou
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature version.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
log.Fatalf("Failed to create new OSS client: %v", err)
}
// Specify the bucket name.
bucketName := "examplebucket"
stat, err := client.GetBucketStat(bucketName)
if err != nil {
log.Fatalf("Failed to get bucket statistics for bucket '%s': %v", bucketName, err)
}
// Total storage capacity of the bucket in bytes.
log.Printf("Bucket Stat Storage: %d bytes\n", stat.Storage)
// Total number of objects in the bucket.
log.Printf("Bucket Stat Object Count: %d\n", stat.ObjectCount)
}BucketStat fields
The GetBucketStat call returns a BucketStat struct. The fields are grouped below by purpose.
Summary
| Field | Description |
|---|---|
Storage | Total storage capacity of the bucket in bytes. |
ObjectCount | Total number of objects in the bucket. |
LastModifiedTime | Time when the storage statistics were collected by this call. UNIX timestamp, in seconds. |
By storage class
| Field | Description |
|---|---|
StandardStorage | Storage capacity of Standard objects in bytes. |
StandardObjectCount | Number of Standard objects. |
InfrequentAccessStorage | Billable storage capacity of Infrequent Access objects in bytes. |
InfrequentAccessRealStorage | Actual storage capacity of Infrequent Access objects in bytes. |
InfrequentAccessObjectCount | Number of Infrequent Access 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. |
Pending uploads and live channels
| Field | Description |
|---|---|
MultipartUploadCount | Number of multipart uploads that have been initiated but not completed or aborted. |
LiveChannelCount | Number of LiveChannels in the bucket. |
References
BucketStat — Go SDK API reference