A bucket is a container for objects. You can use the Object Storage Service (OSS) Go SDK to retrieve bucket information. This information can include the access tracking status, region, creation date, access control list (ACL), owner's name and ID, storage class, disaster recovery type, public endpoint, internal same-region endpoint, cross-region replication status, versioning status, and encryption method. You can use this information to perform related operations. For example, you can access an object by combining its name with the bucket's public endpoint.
Usage 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.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Configure OSSClient instances.
To query information about a bucket, you must have the
oss:GetBucketInfopermission. For more information, see Attach a custom policy to a RAM user.Go SDK V2.2.5 and later returns all properties shown in the following sample code.
Sample code
The following code shows how to retrieve bucket information.
package main
import (
"log"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Get 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.
// Set yourEndpoint to the endpoint of the bucket. For example, for the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
// Set yourRegion to the region where the bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
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. For 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) // Print the bucket name.
log.Printf("Bucket Info Location: %s\n", res.BucketInfo.Location) // Print the region where the bucket is located.
}
List of common bucket information
Parameter | Description |
BucketInfo.Name | The name of the bucket. |
BucketInfo.AccessMonitor | The access tracking status of the bucket. |
BucketInfo.Location | The region where the bucket is located. |
BucketInfo.CreationDate | The creation date of the bucket. |
BucketInfo.ExtranetEndpoint | The public endpoint of the bucket. |
BucketInfo.IntranetEndpoint | The internal same-region endpoint for accessing the bucket from an ECS instance. |
BucketInfo.ACL | The ACL of the bucket. |
BucketInfo.RedundancyType | The data disaster recovery type of the bucket. |
BucketInfo.Owner | This parameter includes the following subparameters:
|
BucketInfo.StorageClass | The storage class of the bucket. |
BucketInfo.SseRule | This parameter includes the following subparameters:
|
BucketInfo.Versioning | The versioning status of the bucket. |
BucketInfo.CrossRegionReplication | The cross-region replication status of the bucket. |
References
For more information about buckets, see Overview of buckets.
For more information about the API for bucket information, see BucketInfo.