TOS 支持为对象设置标签,并根据对象标签,完成特定对象的生命周期管理。本文介绍如何管理对象标签。
tos:PutObjectTagging
权限,具体操作,请参见权限配置指南。tos:GetObjectTagging
权限,具体操作,请参见权限配置指南。tos:DeleteObjectTagging
权限,具体操作,请参见权限配置指南。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 if clientErr, ok := err.(*tos.TosClientError); ok { fmt.Println("Error:", clientErr.Error()) fmt.Println("Client Cause Err:", clientErr.Cause.Error()) } else { fmt.Println("Error:", err) } panic(err) } } func main() { var ( accessKey = os.Getenv("TOS_ACCESS_KEY") secretKey = os.Getenv("TOS_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 ***" // 指定的 ObjectKey objectKey = "*** Provide your object name ***" ctx = context.Background() ) // 初始化客户端 client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey))) checkErr(err) // 设置对象指定 Tag tags := []tos.Tag{ { Key: "key1", Value: "value1", }, { Key: "key2", Value: "value2", }, } output, err := client.PutObjectTagging(ctx, &tos.PutObjectTaggingInput{ Bucket: bucketName, Key: objectKey, // 如果桶开启的多版本,通过设置 VersionID 来设置指定版本对象 Tag VersionID: "", TagSet: tos.TagSet{Tags: tags}, }) checkErr(err) fmt.Println("PutObjectTagging Request ID:", output.RequestID) }
设置对象标签后,您可以根据获取对象的标签信息。当桶开启版本控制时,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 if clientErr, ok := err.(*tos.TosClientError); ok { fmt.Println("Error:", clientErr.Error()) fmt.Println("Client Cause Err:", clientErr.Cause.Error()) } else { fmt.Println("Error:", err) } panic(err) } } func main() { var ( accessKey = os.Getenv("TOS_ACCESS_KEY") secretKey = os.Getenv("TOS_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 ***" // 指定的 ObjectKey objectKey = "*** Provide your object key ***" ctx = context.Background() ) // 初始化客户端 client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey))) checkErr(err) // 设置对象 Tag output, err := client.GetObjectTagging(ctx, &tos.GetObjectTaggingInput{ Bucket: bucketName, Key: objectKey, // 如果桶开启的多版本,通过设置 VersionID 来设置获取版本对象 Tag VersionID: "", }) checkErr(err) fmt.Println("GetObjectTagging Request ID:", output.RequestID) for key, value := range output.TagSet.Tags { fmt.Printf("Object Key:%s,Key:%s, Value:%s\n", objectKey, key, value) } }
您可以根据需要删除不需要的标签信息。当桶开启多版本时,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 if clientErr, ok := err.(*tos.TosClientError); ok { fmt.Println("Error:", clientErr.Error()) fmt.Println("Client Cause Err:", clientErr.Cause.Error()) } else { fmt.Println("Error:", err) } panic(err) } } func main() { var ( accessKey = os.Getenv("TOS_ACCESS_KEY") secretKey = os.Getenv("TOS_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 ***" // 指定的 ObjectKey objectKey = "*** Provide your object key ***" ctx = context.Background() ) // 初始化客户端 client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey))) checkErr(err) // 设置对象 Tag output, err := client.DeleteObjectTagging(ctx, &tos.DeleteObjectTaggingInput{ Bucket: bucketName, Key: objectKey, // 如果桶开启的多版本,通过设置 VersionID 来删除指定版本对象 Tag VersionID: "", }) checkErr(err) fmt.Println("DeleteObjectTagging Request ID:", output.RequestID) }
关于对象标签的 API 文档,请参见 PutObjectTagging。