You need to enable JavaScript to run this app.
导航
管理事件通知(Go SDK)
最近更新时间:2024.02.04 18:31:01首次发布时间:2023.01.18 16:55:22

如果您希望实时掌握在 TOS 中的资源变动,可以通过 Go SDK 设置事件通知规则。

注意事项

  • 只有桶 Owner 才可以创建事件通知服务。
  • 设置事件通知需要依赖函数服务,您需要提前在火山引擎中创建函数服务并发布,具体操作,请参见创建函数发布函数

设置事件通知

以下代码用于设置桶的事件通知规则。

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

相关文档

关于事件通知的更多信息,请参见事件通知