You can use tags to identify buckets used for different purposes and manage these buckets by category.

Configure tags for a bucket

The following code provides an example on how to configure tags for a bucket:

package main

import (
  "fmt"
  "os"

  "github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
  // Create an OSSClient instance. 
  // Set yourEndpoint to 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 the actual endpoint. 
  // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access Object Storage Service (OSS) because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine Q&M. To create a RAM user, log on to the RAM console. 
  client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
  if err != nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }

  // Initialize tagging configurations. 
  tag1 := oss.Tag{
    Key:   "key1",
    Value: "value1",
  }
  tag2 := oss.Tag{
    Key:   "key2",
    Value: "value2",
  }
  tagging := oss.Tagging{
    Tags: []oss.Tag{tag1, tag2},
  }
  // Specify the bucket name. Example: examplebucket. 
  // Configure tags for the bucket. 
  err = client.SetBucketTagging("examplebucket", tagging)
  if err != nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }
}

Query the tags of a bucket

The following code provides an example on how to query the tags configured for a bucket:

package main

import (
  "fmt"
  "os"

  "github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
  // Create an OSSClient instance. 
  // Set yourEndpoint to 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 the actual endpoint. 
  // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine Q&M. To create a RAM user, log on to the RAM console. 
  client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
  if err != nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }

  // Specify the bucket name. Example: examplebucket. 
  // Query the tags of the bucket. 
  ret, err := client.GetBucketTagging("examplebucket")
  if err != nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }
  // Display the number of returned tags. 
  fmt.Println("Tag length: ", len(ret.Tags))
}

List buckets that have specified tags

The following code provides an example on how to list buckets that have specified tags:

package main

import (
  "fmt"
  "os"

  "github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
  // Create an OSSClient instance. 
  // Set yourEndpoint to 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 the actual endpoint. 
  // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine Q&M. To create a RAM user, log on to the RAM console. 
  client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
  if err != nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }

  // Query buckets by specifying the TagKey parameter. 
  ret, err := client.ListBuckets(oss.TagKey("yourTaggingKey"))
  if err != nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }
  // Display the information about the buckets. 
  for _, bucket := range ret.Buckets{
    fmt.Println("bucket:", bucket)
  }
  // Query buckets by specifying the TagKey and TagValue parameters. 
  // The TagValue parameter must be configured together with the TagKey parameter and can be unspecified. If you do not specify the TagValue parameter, buckets are listed based on only the TagKey parameter. 
  res, err := client.ListBuckets(oss.TagKey("yourTaggingKey"), oss.TagValue("yourTaggingValue"))
  if err != nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }
  // Display the information about the listed buckets. 
  for _, b := range res.Buckets{
    fmt.Println("bucket:", b)
  }
}

Delete the tags of a bucket

The following code provides an example on how to delete the tags configured for a bucket:

package main

import (
  "fmt"
  "os"

  "github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
  // Create an OSSClient instance. 
  // Set yourEndpoint to 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 the actual endpoint. 
  // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine Q&M. To create a RAM user, log on to the RAM console. 
  client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
  if err != nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }
  // Specify the bucket name. Example: examplebucket. 
  // Delete the tags of the bucket. 
  err = client.DeleteBucketTagging("examplebucket")
  if err != nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }
}

References

  • For more information about how to configure bucket tags, see PutBucketTags.
  • For more information about how to query the tags configured for a bucket, see GetBucketTags.
  • For more information about how to delete the tags configured for a bucket, see DeleteBucketTags.