Object Storage Service (OSS) allows you to configure bucket tagging to classify and manage buckets. For example, you can use this feature to list buckets that have specific tags.

Note
  • Only the bucket owner or authorized Resource Access Management (RAM) users can configure tagging for the bucket. Otherwise, 403 Forbidden is returned with the error code AccessDenied.
  • You can configure up to 20 tags for a bucket. A tag is a key-value pair.
  • The key of a tag can be up to 64 characters in length and cannot start with http://, https://, or Aliyun. The key of a tag cannot be empty.
  • The value of a tag can be up to 128 characters in length and can be empty.
  • The key and value of a tag must be UTF-8-encoded.
  • When you use PutBucketTags to configure a tag for a bucket, the new tag overwrites the exiting tag.

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. 
  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},
  }
  // Configure tags for the bucket. 
  err = client.SetBucketTagging("yourBucketName", 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. 
  client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
  if err != nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }

  // Query the tags of the bucket. 
  ret, err := client.GetBucketTagging("yourBucketName")
  if err != nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }
  // Display the number of the 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. 
  client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
  if err != nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }

  // Query buckets by specifying the key of a tag. 
  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 key and the value of a tag. 
  // You must specify the TagKey and TagValue at the same time. You can also leave the two parameters empty. If you do not specify the two parameters, the buckets are listed without filtering their tag values. 
  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 of a bucket:

package main

import (
  "fmt"
  "os"

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

func main() {
  // Create an OSSClient instance. 
  client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
  if err != nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }
  // Delete the tags of the object. 
  err = client.DeleteBucketTagging("yourBucketName")
  if err != nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }
}