All Products
Search
Document Center

Object Storage Service:Query information about a bucket

Last Updated:Dec 29, 2023

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.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS by using other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about the regions and endpoints supported by OSS, 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 Initialization.

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

Sample code

The following sample code provides an example on how to query information about a bucket:

package main

import (
    "fmt"
    "os"
    "github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
    /// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
    provider, err := oss.NewEnvironmentVariableCredentialsProvider()
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // 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. 
    client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    
    // Specify the name of the bucket. Example: examplebucket. 
    res, err := client.GetBucketInfo("examplebucket")
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    fmt.Println("Bucket Info Name: ", res.BucketInfo.Name)
    // Query the access tracking state of the bucket. 
    fmt.Println("Bucket Info AccessMonitor: ", res.BucketInfo.AccessMonitor)
    // Query the region in which the bucket is located. 
    fmt.Println("Bucket Info Location: ", res.BucketInfo.Location)
    // Query the date when the bucket was created. 
    fmt.Println("Bucket Info CreationDate: ", res.BucketInfo.CreationDate)
    // Query the ACL of the bucket. 
    fmt.Println("Bucket Info ACL: ", res.BucketInfo.ACL)
    // Query the user ID of the bucket owner. 
    fmt.Println("Bucket Info Owner Id: ", res.BucketInfo.Owner.ID)
    // Query the name of the bucket owner, which is the same as the user ID of the bucket owner. 
    fmt.Println("Bucket Info Owner DisplayName: ", res.BucketInfo.Owner.DisplayName)
    // Query the storage class of the bucket. 
    fmt.Println("Bucket Info StorageClass: ", res.BucketInfo.StorageClass)
    // Query the redundancy type of the bucket. 
    fmt.Println("Bucket Info RedundancyType: ", res.BucketInfo.RedundancyType)
    // Query the public endpoint that is used to access the bucket over the Internet. 
    fmt.Println("Bucket Info ExtranetEndpoint: ", res.BucketInfo.ExtranetEndpoint)
    // Query the internal endpoint that is used to access the bucket from Elastic Compute Service (ECS) instances that reside in the same region as the bucket. 
    fmt.Println("Bucket Info IntranetEndpoint: ", res.BucketInfo.IntranetEndpoint)
    // Query the CRR state of the bucket. 
    fmt.Println("Bucket Info CrossRegionReplication: ", res.BucketInfo.CrossRegionReplication)
    if res.BucketInfo.Versioning !="" {
        // Query the versioning state of the bucket. 
        fmt.Println("Bucket Info Versioning: ", res.BucketInfo.Versioning)
    }
    if res.BucketInfo.SseRule.KMSDataEncryption !="" {
        // Check whether the bucket is encrypted by using Key Management Service (KMS). 
        fmt.Println("Bucket Info SseRule KMSDataEncryption: ", res.BucketInfo.SseRule.KMSDataEncryption)
    }
    if res.BucketInfo.SseRule.KMSMasterKeyID !="" {
        // Query the ID of the customer master key (CMK) used for KMS encryption. 
        fmt.Println("Bucket Info SseRule KMSMasterKeyID: ", res.BucketInfo.SseRule.KMSMasterKeyID)
    }
    if res.BucketInfo.SseRule.SSEAlgorithm !="" {
        // Query the default server-side encryption method that is used to encrypt the bucket. 
        fmt.Println("Bucket Info SseRule SSEAlgorithm: ", res.BucketInfo.SseRule.SSEAlgorithm)
    }
}

References

  • For more information about buckets, see Overview.

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