You need to enable JavaScript to run this app.
导航
管理桶生命周期(Go SDK)
最近更新时间:2024.02.04 18:31:01首次发布时间:2023.01.18 16:55:21

您可以通过 SDK 使用对象存储的生命周期管理功能,实现对象的过期删除达到节约存储成本的目的。配置生命周期规则时,可指定规则生效的对象前缀或者对象标签。如果您同时指定了两种生效条件,则对象需满足设置的所有条件,才会应用该生命周期规则。

设置生命周期

注意

  • 设置生命周期规则前,您必须具有 tos:PutLifecycleConfiguration 权限,具体操作,请参见权限配置指南
  • 如果已对桶配置过生命周期规则,则执行此操作将覆盖原有配置。如果您需要追加生命周期规则,请先使用 GetLifecycleConfigutation 获取当前的规则,再追加写入新规则,最后再调用 PutLifecycleConfiguration 更新规则。

示例代码

以下代码用于设置存储桶的生命周期规则。

package main

import (
   "context"
   "fmt"

   "github.com/volcengine/ve-tos-golang-sdk/v2/tos"
   "github.com/volcengine/ve-tos-golang-sdk/v2/tos/enum"
)

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 ***"
      ctx        = context.Background()
   )
   // 初始化客户端
   client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey)))
   checkErr(err)
   lifeCycleRule := tos.LifecycleRule{
      // 规则 ID
      ID: "rule1",
      // 指定规则适用的前缀,匹配前缀
      Prefix: "prefix-",
      // 规则状态,标示规则是否启用
      Status: enum.LifecycleStatusEnabled,
      // 对象的过期时间
      // Days: 指定在对象过期多少天后进行操作
      // Date: 指定对象在哪一天过期
      Expiration: &tos.Expiration{
         Days: 30,
      },
      // 对象标签
      Tag: []tos.Tag{{
         Key:   "key",
         Value: "value",
      }},
   }
   // 设置生命周期规则
   putOutput, err := client.PutBucketLifecycle(ctx, &tos.PutBucketLifecycleInput{
      Bucket: bucketName,
      Rules:  []tos.LifecycleRule{lifeCycleRule},
   })
   checkErr(err)
   fmt.Println("PutBucketLifecycle Request ID:", putOutput.RequestID)
   
}

获取生命周期配置

注意

  • 获取桶设置生命周期规则前,您必须具有 tos:GetLifecycleConfiguration 权限,具体操作,请参见权限配置指南
  • 关于生命周期规则的更多介绍,请参见桶生命周期规则

示例代码

以下代码用于获取存储桶的生命周期规则。

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 ***"
      ctx        = context.Background()
   )
   // 初始化客户端
   client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey)))
   checkErr(err)
   // 查询生命周期规则
   getOutput, err := client.GetBucketLifecycle(ctx, &tos.GetBucketLifecycleInput{Bucket: bucketName})
   checkErr(err)
   fmt.Println("GetBucketLifecycle Request ID:", getOutput.RequestID)
   fmt.Println("GetBucketLifecycle Rules Length:", len(getOutput.Rules))
   for _, rule := range getOutput.Rules {
      // 规则 ID
      fmt.Println("Rule ID:", rule.ID)
      // 指定规则适用的前缀,匹配前缀
      fmt.Println("Rule Prefix:", rule.Prefix)
      // 规则状态,标示规则是否启用
      fmt.Println("Rule Status:", rule.Status)
      // 存储类型转换属性,描述对象的存储类型何时进行转换
      fmt.Println("Rule Transitions:", rule.Transitions)
      // 对象的过期时间
      fmt.Println("Rule Expiration:", rule.Expiration)
      // 非当前版本存储类型何时进行转换
      fmt.Println("Rule NonCurrentVersionTransition:", rule.NonCurrentVersionTransition)
      // 非当前版本对象何时过期
      fmt.Println("Rule NoCurrentVersionExpiration:", rule.NoCurrentVersionExpiration)
      // 标签
      for key, value := range rule.Tag {
         fmt.Println("Rule Tag Key:", key)
         fmt.Println("Rule Tag Value:", value)
      }
      // 非完成上传的分片上传最长保留时间
      fmt.Println("Rule AbortInCompleteMultipartUpload:", rule.AbortInCompleteMultipartUpload)
   }
}

删除生命周期规则

注意

删除桶生命周期规则前,您必须具有 tos:PutLifecycleConfiguration 权限,具体操作,请参见权限配置指南

示例代码

以下代码用于设置存储桶的生命周期规则:

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 ***"
      ctx        = context.Background()
   )
   // 初始化客户端
   client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey)))
   checkErr(err)

   // 删除生命周期规则
   deleteOutput, err := client.DeleteBucketLifecycle(ctx, &tos.DeleteBucketLifecycleInput{Bucket: bucketName})
   checkErr(err)
   fmt.Println("DeleteBucketLifecycle Request ID:", deleteOutput.RequestID)
}

相关文档

关于生命周期规则的更多信息,请参见设置生命周期规则