This topic describes how to use Object Storage Service (OSS) SDK for Go to query information about a bucket, 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.
Usage notes
The sample code in this topic uses the region ID
cn-hangzhou, which corresponds to the China (Hangzhou) region. By default, a public endpoint is used to access resources in a bucket. If you access the bucket from other Alibaba Cloud services that are deployed in the same region, use the 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 the access credentials, see Configure access credentials.
To query information about a bucket, you must have the
oss:GetBucketInfopermission. For more information, see Attach a custom policy to a RAM user.
Method
func (c *Client) GetBucketInfo(ctx context.Context, request *GetBucketInfoRequest, optFns ...func(*Options)) (*GetBucketInfoResult, error)Request parameters
Parameter | Type | Description |
ctx | context.Context | The context of the request, which can be used to specify the total duration of the request. |
request | *GetBucketInfoRequest | Specifies the parameters of a specific operation. For more information, see GetBucketInfoRequest. |
optFns | ...func(*Options) | Optional. The operation-level parameter. For more information, see Options. |
Response parameters
Response parameter | Type | Description |
result | *GetBucketInfoResult | The response to the operation. This parameter is valid when the value of err is nil. For more information, see GetBucketInfoResult. |
err | error | The status of the request. If the request fails, the value of err cannot be nil. |
Examples
The following sample code provides an example on how to query information about a bucket:
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"
)
// Specify the global variables.
var (
region string // The region.
bucketName string // The name of the bucket.
)
// Specify the init function used to initialize command line parameters.
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&bucketName, "bucket", "", "The `name` of the bucket.")
}
func main() {
// Parse command line parameters.
flag.Parse()
// Check whether the bucket name is empty.
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
// Check whether the region is empty.
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
// Load the default configurations and specify the credential provider and region.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
// Create an OSS client.
client := oss.NewClient(cfg)
// Create a request to query information about the bucket.
request := &oss.GetBucketInfoRequest{
Bucket: oss.Ptr(bucketName), // The name of the bucket.
}
// Execute the request to query information about the bucket and process the result.
result, err := client.GetBucketInfo(context.TODO(), request)
if err != nil {
log.Fatalf("failed to get bucket info %v", err)
}
// Display the information about the bucket.
log.Printf("get bucket info result:%v\n", result.BucketInfo)
}
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 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.StorageClass | The storage class of the bucket. |
BucketInfo.SseRule | The following parameters are included:
|
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 Buckets.
For the complete sample code that is used to query information about a bucket, visit GitHub.
For more information about the API operation that you can call to query information about a bucket, see GetBucketInfo.