All Products
Search
Document Center

Object Storage Service:Query the storage usage of a bucket using OSS SDK for Go 2.0

Last Updated:Mar 20, 2026

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-hangzhou for 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

ParameterTypeDescription
ctxcontext.ContextThe request context. Use this to set a timeout or deadline for the request.
request*GetBucketStatRequestThe request parameters, including the bucket name. For details, see GetBucketStatRequest.
optFns...func(*Options)Optional operation-level options. For details, see Options.

Response parameters

ParameterTypeDescription
result*GetBucketStatResultThe response. Valid when err is nil. For details, see GetBucketStatResult.
errerrorThe 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(&region, "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

FieldDescription
StorageTotal storage usage of the bucket, in bytes. Includes all storage classes.
ObjectCountTotal number of objects in the bucket, across all storage classes.
MultipartUploadCountNumber of multipart upload tasks that have been initiated but are not yet completed or canceled.
LiveChannelCountNumber of LiveChannels in the bucket.
LastModifiedTimeUNIX 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.

FieldDescription
StandardStorageStorage usage of Standard objects, in bytes.
StandardObjectCountNumber of Standard objects.
InfrequentAccessStorageBilled storage usage of Infrequent Access (IA) objects, in bytes.
InfrequentAccessRealStorageActual storage usage of IA objects, in bytes.
InfrequentAccessObjectCountNumber of IA objects.
ArchiveStorageBilled storage usage of Archive objects, in bytes.
ArchiveRealStorageActual storage usage of Archive objects, in bytes.
ArchiveObjectCountNumber of Archive objects.
ColdArchiveStorageBilled storage usage of Cold Archive objects, in bytes.
ColdArchiveRealStorageActual storage usage of Cold Archive objects, in bytes.
ColdArchiveObjectCountNumber of Cold Archive objects.

What's next