TOS 支持桶标签功能,标签可用于标识云资源,帮助您从不同维度(如用途、类型、所有者、环境等)对具有相同特征的存储桶进行标记和分类,便于管理。
以下代码用于设置指定存储桶标签。
package main import ( "context" "fmt" "github.com/volcengine/ve-tos-golang-sdk/v2/tos" ) func checkErr(err error) { if err != nil { if serverErr, ok := err.(*tos.TosServerError); ok { fmt.Println("Error:", serverErr.Error()) fmt.Println("Request ID:", serverErr.RequestID) fmt.Println("Response Status Code:", serverErr.StatusCode) fmt.Println("Response Header:", serverErr.Header) fmt.Println("Response Err Code:", serverErr.Code) fmt.Println("Response Err Msg:", serverErr.Message) } else { fmt.Println("Error:", err) } panic(err) } } func main() { var ( accessKey = "*** Provide your access key ***" secretKey = "*** Provide your secret key ***" // Bucket 对应的 Endpoint,以华北2(北京)为例:https://tos-cn-beijing.volces.com endpoint = "https://tos-cn-beijing.volces.com" region = "cn-beijing" // 填写 BucketName bucketName = "*** Provide your bucket name ***" ctx = context.Background() ) // 初始化客户端 client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey))) checkErr(err) input := &tos.PutBucketTaggingInput{ Bucket: bucketName, // 设置标签 TagSet: tos.TagSet{Tags: []tos.Tag{ { Key: "key1", Value: "value1", }, { Key: "key2", Value: "value2", }, }}, } output, err := client.PutBucketTagging(ctx, input) checkErr(err) fmt.Println("PutBucketTagging Request ID:", output.RequestID) }
以下代码用于获取指定存储桶标签。
package main import ( "context" "fmt" "github.com/volcengine/ve-tos-golang-sdk/v2/tos" ) func checkErr(err error) { if err != nil { if serverErr, ok := err.(*tos.TosServerError); ok { fmt.Println("Error:", serverErr.Error()) fmt.Println("Request ID:", serverErr.RequestID) fmt.Println("Response Status Code:", serverErr.StatusCode) fmt.Println("Response Header:", serverErr.Header) fmt.Println("Response Err Code:", serverErr.Code) fmt.Println("Response Err Msg:", serverErr.Message) } else { fmt.Println("Error:", err) } panic(err) } } func main() { var ( accessKey = "*** Provide your access key ***" secretKey = "*** Provide your secret key ***" // Bucket 对应的 Endpoint,以华北2(北京)为例:https://tos-cn-beijing.volces.com endpoint = "https://tos-cn-beijing.volces.com" region = "cn-beijing" // 填写 BucketName bucketName = "*** Provide your bucket name ***" ctx = context.Background() ) // 初始化客户端 client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey))) checkErr(err) input := &tos.GetBucketTaggingInput{ Bucket: bucketName, } output, err := client.GetBucketTagging(ctx, input) checkErr(err) fmt.Println("GetBucketTagging Request ID:", output.RequestID) for _, tag := range output.TagSet.Tags { fmt.Println("key:", tag.Key, " value:", tag.Value) } }
以下代码用于删除指定存储桶标签。
package main import ( "context" "fmt" "github.com/volcengine/ve-tos-golang-sdk/v2/tos" ) func checkErr(err error) { if err != nil { if serverErr, ok := err.(*tos.TosServerError); ok { fmt.Println("Error:", serverErr.Error()) fmt.Println("Request ID:", serverErr.RequestID) fmt.Println("Response Status Code:", serverErr.StatusCode) fmt.Println("Response Header:", serverErr.Header) fmt.Println("Response Err Code:", serverErr.Code) fmt.Println("Response Err Msg:", serverErr.Message) } else { fmt.Println("Error:", err) } panic(err) } } func main() { var ( accessKey = "*** Provide your access key ***" secretKey = "*** Provide your secret key ***" // Bucket 对应的 Endpoint,以华北2(北京)为例:https://tos-cn-beijing.volces.com endpoint = "https://tos-cn-beijing.volces.com" region = "cn-beijing" // 填写 BucketName bucketName = "*** Provide your bucket name ***" ctx = context.Background() ) // 初始化客户端 client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey))) checkErr(err) input := &tos.DeleteBucketTaggingInput{ Bucket: bucketName, } output, err := client.DeleteBucketTagging(ctx, input) checkErr(err) fmt.Println("DeleteBucketTagging Request ID:", output.RequestID) }
关于桶标签的更多信息,请参见设置桶标签。