如果您希望实时掌握在 TOS 中的资源变动,可以通过 Go SDK 设置事件通知规则。
以下代码用于设置桶的事件通知规则。
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 = 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 ***" ctx = context.Background() ) // 初始化客户端 client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey))) checkErr(err) input := &tos.PutBucketNotificationInput{Bucket: bucketName, CloudFunctionConfigurations: []tos.CloudFunctionConfiguration{ { // 事件通知名称 ID: "TestNotification", // 需要订阅的事件 Events: []string{"tos:ObjectCreated:Put"}, // 设置匹配对象的前缀信息和后缀信息 Filter: tos.Filter{Key: tos.FilterKey{Rules: []tos.FilterRule{{ Name: "prefix", Value: "test-", }, { Name: "suffix", Value: "-ci", }}}}, // 设置订阅事件的函数服务 CloudFunction: "Provide the ID of your cloud function", }, }} out, err := client.PutBucketNotification(ctx, input) checkErr(err) fmt.Println("Request ID:", out.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 = 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 ***" ctx = context.Background() ) // 初始化客户端 client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey))) checkErr(err) out, err := client.GetBucketNotification(ctx, &tos.GetBucketNotificationInput{Bucket: bucketName}) checkErr(err) fmt.Println("Request ID:", out.RequestID) for _, conf := range out.CloudFunctionConfigurations { // 事件通知名称 fmt.Println("Cloud Function Configuration ID:", conf.ID) // 订阅的事件 fmt.Println("Cloud Function Configuration Events", conf.Events) // 设置匹配对象的前缀信息和后缀信息 fmt.Println("Cloud Function Configuration Filter", conf.Filter) // 设置订阅事件的函数服务 fmt.Println("Cloud Function Configuration CloudFunction", conf.CloudFunction) } }
关于事件通知的更多信息,请参见事件通知。