Query information about a bucket (OSS SDK for Go 1.0)

Updated at:

A bucket is a container that is used to store objects in Object Storage Service (OSS). You can use OSS SDK for Go to query bucket information such as the access tracking state, region, creation date, access control list (ACL), name and ID of the owner, storage class, redundancy type, public endpoint, internal endpoint, cross-region replication (CRR) state, versioning state, and encryption method. This helps you perform subsequent operations. For example, you can use the obtained public endpoint together with the name of an object to access the object.

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

  • To query information about a bucket, you must have the oss:GetBucketInfo permission. For more information, see Grant a custom policy.

  • Object Storage Service (OSS) SDK for Go 2.2.5 or later supports all attributes that are included in the following sample code.

Examples

The following sample code provides an example on how to query information about a 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. 
	// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
	// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	// Specify the version of the signature algorithm.
	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 name of the bucket. Example: examplebucket. 
	bucketName := "examplebucket"
	res, err := client.GetBucketInfo(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket info for bucket '%s': %v", bucketName, err)
	}

	log.Printf("Bucket Info Name: %s\n", res.BucketInfo.Name)         // Display the name of the bucket.
	log.Printf("Bucket Info Location: %s\n", res.BucketInfo.Location) // Display the region in which the bucket is located.
}

Common parameters for bucket information

Parameter

Description

BucketInfo.Name

The name of the bucket.

BucketInfo.AccessMonitor

The access tracking status of the bucket.

BucketInfo.Location

The region in which the OSS bucket is located.

BucketInfo.CreationDate

The date when the bucket was created.

BucketInfo.ExtranetEndpoint

The public endpoint of the bucket.

BucketInfo.IntranetEndpoint

The internal endpoint that can be used by an Elastic Compute Service (ECS) instance that resides in the same region in which the bucket is located to access the bucket.

BucketInfo.ACL

The ACL of the bucket.

BucketInfo.RedundancyType

The redundancy type of the bucket.

BucketInfo.Owner

The following parameters are included:

  • BucketInfo.Owner.ID: the ID of the bucket owner.

  • BucketInfo.Owner.DisplayName: the name of the bucket owner.

BucketInfo.StorageClass

The storage class of the bucket.

BucketInfo.SseRule

The following parameters are included:

  • BucketInfo.SseRule.KMSDataEncryption: whether to use Key Management Service (KMS) to encrypt data in the bucket.

  • BucketInfo.SseRule.KMSMasterKeyID: the ID of the KMS key.

  • BucketInfo.SseRule.SSEAlgorithm: the default server-side encryption method.

BucketInfo.Versioning

The versioning status of the bucket.

BucketInfo.CrossRegionReplication

The cross-region replication (CRR) status of the bucket.

References

  • For more information about buckets, see Bucket overview.

  • For more information about the API operation that you can call to query information about a bucket, see GetBucketInfo.