All Products
Search
Document Center

Object Storage Service:Manage bucket tagging using OSS SDK for Go 2.0

Last Updated:Mar 20, 2026

Manage bucket tags using the OSS SDK for Go v2. Tags are key-value pairs attached to a bucket, commonly used for cost allocation, resource grouping, and access control.

Operations

OperationSDK methodDescription
Set tagsPutBucketTagsAdds or replaces all tags on a bucket
Get tagsGetBucketTagsReturns all tags currently set on a bucket
Delete tagsDeleteBucketTagsRemoves one, multiple, or all tags from a bucket

Usage notes

  • The examples in this topic use the China (Hangzhou) region (cn-hangzhou) with a public endpoint. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint instead. For endpoint details, see Regions and endpoints.

  • Credentials are read from environment variables. For setup instructions, see Configure access credentials.

Method signatures

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

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

func (c *Client) DeleteBucketTags(ctx context.Context, request *DeleteBucketTagsRequest, optFns ...func(*Options)) (*DeleteBucketTagsResult, error)

Parameters

ParameterTypeDescription
ctxcontext.ContextControls the total duration of the request.
request*PutBucketTagsRequest | *GetBucketTagsRequest | *DeleteBucketTagsRequestOperation-specific request parameters. See PutBucketTagsRequest, GetBucketTagsRequest, DeleteBucketTagsRequest.
optFns...func(*Options)Optional operation-level configuration. See Options.

Return values

ValueTypeDescription
result*PutBucketTagsResult | *GetBucketTagsResult | *DeleteBucketTagsResultThe response. Valid only when err is nil. See PutBucketTagsResult, GetBucketTagsResult, DeleteBucketTagsResult.
errerrorNon-nil if the request fails.

Examples

All examples accept --region and --bucket flags and load credentials from environment variables.

Set tags on a bucket

Construct a PutBucketTagsRequest with a Tagging field containing a TagSet. Each tag is a KeyValue pair.

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)
}

Get tags on a bucket

GetBucketTagsRequest requires only the bucket name. The response contains Tagging.TagSet.Tags, a slice of Tag structs, each with a Key and Value.

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 tags from a bucket

DeleteBucketTags supports three deletion modes, controlled by the Tagging field in the request:

GoalTagging field value
Delete a single tagoss.Ptr("k1") — the tag key
Delete multiple tagsoss.Ptr("k1,k2") — comma-separated keys
Delete all tagsOmit the Tagging field

Delete a single tag

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"),       // The key of the tag to delete.
	}

	// 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

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"),    // Comma-separated keys of the tags to delete.
	}

	// 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

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. Omitting Tagging deletes all tags.
	}

	// 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