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
| Operation | SDK method | Description |
|---|---|---|
| Set tags | PutBucketTags | Adds or replaces all tags on a bucket |
| Get tags | GetBucketTags | Returns all tags currently set on a bucket |
| Delete tags | DeleteBucketTags | Removes 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
| Parameter | Type | Description |
|---|---|---|
ctx | context.Context | Controls the total duration of the request. |
request | *PutBucketTagsRequest | *GetBucketTagsRequest | *DeleteBucketTagsRequest | Operation-specific request parameters. See PutBucketTagsRequest, GetBucketTagsRequest, DeleteBucketTagsRequest. |
optFns | ...func(*Options) | Optional operation-level configuration. See Options. |
Return values
| Value | Type | Description |
|---|---|---|
result | *PutBucketTagsResult | *GetBucketTagsResult | *DeleteBucketTagsResult | The response. Valid only when err is nil. See PutBucketTagsResult, GetBucketTagsResult, DeleteBucketTagsResult. |
err | error | Non-nil if the request fails. |
Examples
All examples accept --region and --bucket flags and load credentials from environment variables.
Delete tags from a bucket
DeleteBucketTags supports three deletion modes, controlled by the Tagging field in the request:
| Goal | Tagging field value |
|---|---|
| Delete a single tag | oss.Ptr("k1") — the tag key |
| Delete multiple tags | oss.Ptr("k1,k2") — comma-separated keys |
| Delete all tags | Omit 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(®ion, "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(®ion, "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(®ion, "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)
}