All Products
Search
Document Center

Object Storage Service:Query the storage capacity of a bucket (Go SDK V1)

Last Updated:Mar 20, 2026

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:

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

FieldDescription
StorageTotal storage capacity of the bucket in bytes.
ObjectCountTotal number of objects in the bucket.
LastModifiedTimeTime when the storage statistics were collected by this call. UNIX timestamp, in seconds.

By storage class

FieldDescription
StandardStorageStorage capacity of Standard objects in bytes.
StandardObjectCountNumber of Standard objects.
InfrequentAccessStorageBillable storage capacity of Infrequent Access objects in bytes.
InfrequentAccessRealStorageActual storage capacity of Infrequent Access objects in bytes.
InfrequentAccessObjectCountNumber of Infrequent Access objects.
ArchiveStorageBillable storage capacity of Archive Storage objects in bytes.
ArchiveRealStorageActual storage capacity of Archive Storage objects in bytes.
ArchiveObjectCountNumber of Archive Storage objects.
ColdArchiveStorageBillable storage capacity of Cold Archive objects in bytes.
ColdArchiveRealStorageActual storage capacity of Cold Archive objects in bytes.
ColdArchiveObjectCountNumber of Cold Archive objects.

Pending uploads and live channels

FieldDescription
MultipartUploadCountNumber of multipart uploads that have been initiated but not completed or aborted.
LiveChannelCountNumber of LiveChannels in the bucket.

References