设置生命周期规则
更新时间: 2019-03-14 10:05
您可以通过ObsClient.SetBucketLifecycleConfiguration设置桶的生命周期规则。
设置对象转换策略
以下代码展示了如何设置最新版本对象和历史版本对象的转换策略:
- // 引入依赖包
- import (
- "fmt"
- "obs"
- )
- var ak = "*** Provide your Access Key ***"
- var sk = "*** Provide your Secret Key ***"
- var endpoint = "https://your-endpoint"
- // 创建ObsClient结构体
- var obsClient, _ = obs.New(ak, sk, endpoint)
- func main() {
- input := &obs.SetBucketLifecycleConfigurationInput{}
- input.Bucket = "bucketname"
- var lifecycleRules [2]obs.LifecycleRule
- lifecycleRule0 := obs.LifecycleRule{}
- lifecycleRule0.ID = "rule0"
- lifecycleRule0.Prefix = "prefix0"
- lifecycleRule0.Status = obs.RuleStatusEnabled
- // 指定满足前缀的对象创建30天后转换为低频访问存储,指定满足前缀的对象创建60天后转换为归档存储
- var transitions [2]obs.Transition
- transitions[0] = obs.Transition{}
- transitions[0].Days = 30
- transitions[0].StorageClass = obs.StorageClassWarm
- transitions[1] = obs.Transition{}
- transitions[1].Days = 60
- transitions[1].StorageClass = obs.StorageClassCold
- lifecycleRule0.Transitions = transitions[:]
- lifecycleRules[0] = lifecycleRule0
- lifecycleRule1 := obs.LifecycleRule{}
- lifecycleRule1.Status = obs.RuleStatusEnabled
- lifecycleRule1.ID = "rule1"
- lifecycleRule1.Prefix = "prefix1"
- // 指定满足前缀的对象成为历史版本30天后转换为低频访问存储,指定满足前缀的对象成为历史版本60天后转换为归档存储
- var noncurrentTransitions [2]obs.NoncurrentVersionTransition
- noncurrentTransitions[0] = obs.NoncurrentVersionTransition{}
- noncurrentTransitions[0].NoncurrentDays = 30
- noncurrentTransitions[0].StorageClass = obs.StorageClassWarm
- noncurrentTransitions[1] = obs.NoncurrentVersionTransition{}
- noncurrentTransitions[1].NoncurrentDays = 60
- noncurrentTransitions[1].StorageClass = obs.StorageClassCold
- lifecycleRule1.NoncurrentVersionTransitions = noncurrentTransitions[:]
- lifecycleRules[1] = lifecycleRule1
- input.LifecycleRules = lifecycleRules[:]
- output, err := obsClient.SetBucketLifecycleConfiguration(input)
- if err == nil {
- fmt.Printf("RequestId:%s\n", output.RequestId)
- } else if obsError, ok := err.(obs.ObsError); ok {
- fmt.Printf("Code:%s\n", obsError.Code)
- fmt.Printf("Message:%s\n", obsError.Message)
- }
- }
设置对象过期时间
以下代码展示了如何设置最新版本对象和历史版本对象的过期时间:
- // 引入依赖包
- import (
- "fmt"
- "obs"
- "time"
- )
- var ak = "*** Provide your Access Key ***"
- var sk = "*** Provide your Secret Key ***"
- var endpoint = "https://your-endpoint"
- // 创建ObsClient结构体
- var obsClient, _ = obs.New(ak, sk, endpoint)
- func main() {
- input := &obs.SetBucketLifecycleConfigurationInput{}
- input.Bucket = "bucketname"
- var lifecycleRules [2]obs.LifecycleRule
- lifecycleRule0 := obs.LifecycleRule{}
- lifecycleRule0.ID = "rule0"
- lifecycleRule0.Prefix = "prefix0"
- lifecycleRule0.Status = obs.RuleStatusEnabled
- // 指定满足前缀的对象创建30天后过期,指定满足前缀的对象成为历史版本30天后过期
- lifecycleRule0.Expiration.Days = 30
- lifecycleRule0.NoncurrentVersionExpiration.NoncurrentDays = 30
- lifecycleRules[0] = lifecycleRule0
- lifecycleRule1 := obs.LifecycleRule{}
- lifecycleRule1.Status = obs.RuleStatusEnabled
- lifecycleRule1.ID = "rule1"
- lifecycleRule1.Prefix = "prefix1"
- // 直接指定满足前缀的对象过期时间
- lifecycleRule1.Expiration.Date = time.Now().Add(time.Duration(24*60) * time.Hour)
- lifecycleRules[1] = lifecycleRule1
- input.LifecycleRules = lifecycleRules[:]
- output, err := obsClient.SetBucketLifecycleConfiguration(input)
- if err == nil {
- fmt.Printf("RequestId:%s\n", output.RequestId)
- } else if obsError, ok := err.(obs.ObsError); ok {
- fmt.Printf("Code:%s\n", obsError.Code)
- fmt.Printf("Message:%s\n", obsError.Message)
- }
- }
父主题:生命周期管理