All Products
Search
Document Center

Object Storage Service:Query information about a bucket using OSS SDK for Go 2.0

Last Updated:Mar 20, 2026

Use GetBucketInfo to retrieve metadata about an OSS bucket — including its region, storage class, access control list (ACL), versioning state, encryption configuration, cross-region replication (CRR) state, and owner details.

Prerequisites

Before you begin, ensure that you have:

Usage notes

  • The sample code uses region ID cn-hangzhou (China (Hangzhou)). For other regions, replace it with the appropriate region ID. See Regions and endpoints.

  • To access a bucket from an Elastic Compute Service (ECS) instance in the same region, use the internal endpoint instead of the public endpoint.

Method

func (c *Client) GetBucketInfo(ctx context.Context, request *GetBucketInfoRequest, optFns ...func(*Options)) (*GetBucketInfoResult, error)

Request parameters

ParameterTypeDescription
ctxcontext.ContextThe request context. Use it to set a timeout for the entire operation.
request*GetBucketInfoRequestThe request parameters. See GetBucketInfoRequest.
optFns...func(*Options)Optional operation-level configuration. See Options.

Response parameters

ParameterTypeDescription
result*GetBucketInfoResultThe response. Valid only when err is nil. See GetBucketInfoResult.
errerrorThe request status. A non-nil value means the request failed.

Examples

The following example retrieves information about a bucket and logs the result:

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 where 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)

	client := oss.NewClient(cfg)

	request := &oss.GetBucketInfoRequest{
		Bucket: oss.Ptr(bucketName),
	}

	result, err := client.GetBucketInfo(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to get bucket info: %v", err)
	}

	log.Printf("bucket info: %v\n", result.BucketInfo)
}

For the complete runnable sample, see get_bucket_info.go on GitHub.

BucketInfo fields

The result.BucketInfo struct exposes the following fields:

FieldDescription
BucketInfo.NameThe name of the bucket.
BucketInfo.LocationThe region where the bucket is located.
BucketInfo.CreationDateThe date when the bucket was created.
BucketInfo.StorageClassThe storage class of the bucket.
BucketInfo.RedundancyTypeThe redundancy type of the bucket.
BucketInfo.ACLThe ACL of the bucket.
BucketInfo.VersioningThe versioning state of the bucket.
BucketInfo.AccessMonitorThe access monitoring state of the bucket.
BucketInfo.ExtranetEndpointThe public endpoint of the bucket.
BucketInfo.IntranetEndpointThe internal endpoint for ECS instances in the same region.
BucketInfo.Owner.IDThe ID of the bucket owner.
BucketInfo.Owner.DisplayNameThe display name of the bucket owner.
BucketInfo.CrossRegionReplicationThe cross-region replication (CRR) state of the bucket.
BucketInfo.SseRule.SSEAlgorithmThe default server-side encryption (SSE) method.
BucketInfo.SseRule.KMSMasterKeyIDThe KMS key ID used for encryption.
BucketInfo.SseRule.KMSDataEncryptionWhether Key Management Service (KMS) is used to encrypt data.

What's next