All Products
Search
Document Center

Object Storage Service:Go bucket tagging

Last Updated:Aug 02, 2025

This topic describes how to manage the tags of a bucket.

Usage notes

  • The sample code in this topic uses the China (Hangzhou) region (cn-hangzhou) as an example. By default, a public endpoint is used. If you want to access OSS from other Alibaba Cloud products in the same region as the bucket, use an internal endpoint. For more information about the mappings between 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.

Method

Configure tags for a bucket

func (c *Client) PutBucketTags(ctx context.Context, request *PutBucketTagsRequest, optFns ...func(*Options)) (*PutBucketTagsResult, error)

Query the tags of a bucket

func (c *Client) GetBucketTags(ctx context.Context, request *GetBucketTagsRequest, optFns ...func(*Options)) (*GetBucketTagsResult, error)

Delete the tags of a bucket

func (c *Client) DeleteBucketTags(ctx context.Context, request *DeleteBucketTagsRequest, optFns ...func(*Options)) (*DeleteBucketTagsResult, 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

*PutBucketTagsRequest

Specifies the parameters of a specific API operation. For more information, see PutBucketTagsRequest.

*GetBucketTagsRequest

Specifies the parameters of a specific API operation. For more information, see GetBucketTagsRequest.

*DeleteBucketTagsRequest

Specifies the parameters of a specific API operation. For more information, see DeleteBucketTagsRequest.

optFns

...func(*Options)

Optional. The operation-level parameter. For more information, see Options.

Response parameters

Parameter

Type

Description

result

*PutBucketTagsResult

The response to the operation. This parameter is valid when the value of err is nil. For more information, see PutBucketTagsResult.

*GetBucketTagsRequest

The response to the operation. This parameter is valid when the value of err is nil. For more information, see GetBucketTagsResult.

*DeleteBucketTagsRequest

The response to the operation. This parameter is valid when the value of err is nil. For more information, see DeleteBucketTagsResult.

err

error

The status of the request. If the request fails, the value of err cannot be nil.

Examples

Configure tags for a bucket

The following code provides an example on how to configure tags for 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 in which the bucket is located.
	bucketName string // The name of the bucket.
)

// Specify the init function used to initialize command line parameters.
func init() {
	flag.StringVar(&region, "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 OSSClient instance.
	client := oss.NewClient(cfg)

	// Create a request to configure tags for the bucket.
	request := &oss.PutBucketTagsRequest{
		Bucket: oss.Ptr(bucketName), // The name of the bucket.
		Tagging: &oss.Tagging{
			&oss.TagSet{
				[]oss.Tag{
					{
						Key:   oss.Ptr("k1"), // The key of a tag.
						Value: oss.Ptr("v1"), // The value of a tag.
					},
					{
						Key:   oss.Ptr("k2"), // The key of a tag.
						Value: oss.Ptr("v2"), // The value of a tag.
					},
					{
						Key:   oss.Ptr("k3"), // The key of a tag.
						Value: oss.Ptr("v3"), // The value of a tag.
					},
				},
			},
		},
	}

	// Send the request to configure tags for a bucket.
	result, err := client.PutBucketTags(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to put bucket tags %v", err)
	}

	// Display the result of the tag configuration.
	log.Printf("put bucket tags result:%#v\n", result)
}

Query the tags of a bucket

The following code provides an example on how to query the tags of 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 in which the bucket is located.
	bucketName string // The name of the bucket.
)

// Specify the init function used to initialize command line parameters.
func init() {
	flag.StringVar(&region, "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 OSSClient instance.
	client := oss.NewClient(cfg)

	// Create a request to query the tags of the bucket.
	request := &oss.GetBucketTagsRequest{
		Bucket: oss.Ptr(bucketName), // The name of the bucket.
	}

	// Execute the request to query the tags of the bucket and process the result.
	getResult, err := client.GetBucketTags(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to get bucket tags %v", err)
	}

	// Display the number of bucket tags.
	log.Printf("get bucket tags result:%#v\n", len(getResult.Tagging.TagSet.Tags))
}

Delete the tags of a bucket

Delete a single tag of a bucket

The following code provides an example on how to delete the k1 tag of 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 in which the bucket is located.
	bucketName string // The name of the bucket.
)

// Specify the init function used to initialize command line parameters.
func init() {
	flag.StringVar(&region, "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 OSSClient instance.
	client := oss.NewClient(cfg)

	// Create a request to delete a tag of the bucket.
	request := &oss.DeleteBucketTagsRequest{
		Bucket: oss.Ptr(bucketName), // The name of the bucket.
		Tagging: oss.Ptr("k1"),
	}

	// Execute the request to delete the tag of the bucket and process the result.
	result, err := client.DeleteBucketTags(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to delete bucket tags %v", err)
	}

	// Display the result of the tag deletion.
	log.Printf("delete bucket tags result:%#v\n", result)
}

Delete multiple tags of a bucket

The following code provides an example on how to delete the k1 and k2 tags of 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 in which the bucket is located.
	bucketName string // The name of the bucket.
)

// Specify the init function used to initialize command line parameters.
func init() {
	flag.StringVar(&region, "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 OSSClient instance.
	client := oss.NewClient(cfg)

	// Create a request to delete the two tags of the bucket.
	request := &oss.DeleteBucketTagsRequest{
		Bucket: oss.Ptr(bucketName), // The name of the bucket.
		Tagging: oss.Ptr("k1,k2"),
	}

	// Execute the request to delete the tags of the bucket and process the result.
	result, err := client.DeleteBucketTags(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to delete bucket tags %v", err)
	}

	// Display the result of the tag deletion.
	log.Printf("delete bucket tags result:%#v\n", result)
}

Delete all tags of a bucket

The following code provides an example on how to delete all tags of 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 in which the bucket is located.
	bucketName string // The name of the bucket.
)

// Specify the init function used to initialize command line parameters.
func init() {
	flag.StringVar(&region, "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 OSSClient instance.
	client := oss.NewClient(cfg)

	// Create a request to delete all tags of the bucket.
	request := &oss.DeleteBucketTagsRequest{
		Bucket: oss.Ptr(bucketName), // The name of the bucket.
	}

	// Execute the request to delete all tags of the bucket and process the result.
	result, err := client.DeleteBucketTags(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to delete bucket tags %v", err)
	}

	// Display the result of the tag deletion.
	log.Printf("delete bucket tags result:%#v\n", result)
}

References

  • For more information about the API operation that you can call to configure tags for a bucket, visit PutBucketTags.

  • For more information about the API operation that you can call to query the tags of a bucket, visit GetBucketTags.

  • For more information about the API operation that you can call to delete the tags of a bucket, visit DeleteBucketTags.