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:
An OSS bucket
The
oss:GetBucketInfopermission on the target bucket. For setup instructions, see Attach a custom policy to a RAM userAccess credentials configured as environment variables. See Configure access credentials
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
| Parameter | Type | Description |
|---|---|---|
| ctx | context.Context | The request context. Use it to set a timeout for the entire operation. |
| request | *GetBucketInfoRequest | The request parameters. See GetBucketInfoRequest. |
| optFns | ...func(*Options) | Optional operation-level configuration. See Options. |
Response parameters
| Parameter | Type | Description |
|---|---|---|
| result | *GetBucketInfoResult | The response. Valid only when err is nil. See GetBucketInfoResult. |
| err | error | The 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(®ion, "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:
| Field | Description |
|---|---|
| BucketInfo.Name | The name of the bucket. |
| BucketInfo.Location | The region where the bucket is located. |
| BucketInfo.CreationDate | The date when the bucket was created. |
| BucketInfo.StorageClass | The storage class of the bucket. |
| BucketInfo.RedundancyType | The redundancy type of the bucket. |
| BucketInfo.ACL | The ACL of the bucket. |
| BucketInfo.Versioning | The versioning state of the bucket. |
| BucketInfo.AccessMonitor | The access monitoring state of the bucket. |
| BucketInfo.ExtranetEndpoint | The public endpoint of the bucket. |
| BucketInfo.IntranetEndpoint | The internal endpoint for ECS instances in the same region. |
| BucketInfo.Owner.ID | The ID of the bucket owner. |
| BucketInfo.Owner.DisplayName | The display name of the bucket owner. |
| BucketInfo.CrossRegionReplication | The cross-region replication (CRR) state of the bucket. |
| BucketInfo.SseRule.SSEAlgorithm | The default server-side encryption (SSE) method. |
| BucketInfo.SseRule.KMSMasterKeyID | The KMS key ID used for encryption. |
| BucketInfo.SseRule.KMSDataEncryption | Whether Key Management Service (KMS) is used to encrypt data. |
What's next
For an overview of OSS buckets, see Buckets.
For the full API reference, see GetBucketInfo.