This topic describes how to retrieve the storage capacity of a bucket, and the number and storage capacity of objects for each storage class in the bucket.
Notes
-
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an 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 access credentials, see Configure access credentials.
-
This topic demonstrates creating an OSSClient instance with an OSS endpoint. For alternative configurations, such as using a custom domain or authenticating with credentials from Security Token Service (STS), see Configure a client (Go SDK V1).
-
The following sample code uses properties that are supported in Go SDK 2.2.5 and later.
Sample code
The following code retrieves the storage capacity of a bucket named examplebucket, and the number and storage capacity of objects for each storage class in the bucket.
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.
// Set yourEndpoint to the endpoint of the bucket. For example, for the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, specify the actual endpoint.
// Set yourRegion to the region where the bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, specify the actual region.
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. For example, examplebucket.
bucketName := "examplebucket"
stat, err := client.GetBucketStat(bucketName)
if err != nil {
log.Fatalf("Failed to get bucket statistics for bucket '%s': %v", bucketName, err)
}
// Get the total storage capacity of the bucket in bytes.
log.Printf("Bucket Stat Storage: %d bytes\n", stat.Storage)
// Get the total number of objects in the bucket.
log.Printf("Bucket Stat Object Count: %d\n", stat.ObjectCount)
}
Common storage capacity information
|
Parameter |
Description |
|
Storage |
The total storage capacity of the bucket in bytes. |
|
ObjectCount |
The total number of objects in the bucket. |
|
MultipartUploadCount |
The number of multipart uploads that are initiated but not completed or aborted in the bucket. |
|
LiveChannelCount |
The number of LiveChannels in the bucket. |
|
LastModifiedTime |
The 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 in bytes. |
|
StandardObjectCount |
The number of Standard objects. |
|
InfrequentAccessStorage |
The billable storage capacity of Infrequent Access objects in bytes. |
|
InfrequentAccessRealStorage |
The actual storage capacity of Infrequent Access objects in bytes. |
|
InfrequentAccessObjectCount |
The number of Infrequent Access objects. |
|
ArchiveStorage |
The billable storage capacity of Archive Storage objects in bytes. |
|
ArchiveRealStorage |
The actual storage capacity of Archive Storage objects in bytes. |
|
ArchiveObjectCount |
The number of Archive Storage objects. |
|
ColdArchiveStorage |
The billable storage capacity of Cold Archive objects in bytes. |
|
ColdArchiveRealStorage |
The actual storage capacity of Cold Archive objects in bytes. |
|
ColdArchiveObjectCount |
The number of Cold Archive objects. |
References
-
For more information about the bucket storage capacity API, see BucketStat.