设置生命周期规则

更新时间: 2019-03-14 10:05

您可以通过ObsClient.SetBucketLifecycleConfiguration设置桶的生命周期规则。

设置对象转换策略

以下代码展示了如何设置最新版本对象和历史版本对象的转换策略:

  1. // 引入依赖包
  2. import (
  3. "fmt"
  4. "obs"
  5. )
  6.  
  7. var ak = "*** Provide your Access Key ***"
  8. var sk = "*** Provide your Secret Key ***"
  9. var endpoint = "https://your-endpoint"
  10.  
  11. // 创建ObsClient结构体
  12. var obsClient, _ = obs.New(ak, sk, endpoint)
  13.  
  14. func main() {
  15. input := &obs.SetBucketLifecycleConfigurationInput{}
  16. input.Bucket = "bucketname"
  17. var lifecycleRules [2]obs.LifecycleRule
  18. lifecycleRule0 := obs.LifecycleRule{}
  19. lifecycleRule0.ID = "rule0"
  20. lifecycleRule0.Prefix = "prefix0"
  21. lifecycleRule0.Status = obs.RuleStatusEnabled
  22.  
  23. // 指定满足前缀的对象创建30天后转换为低频访问存储,指定满足前缀的对象创建60天后转换为归档存储
  24.  
  25. var transitions [2]obs.Transition
  26. transitions[0] = obs.Transition{}
  27. transitions[0].Days = 30
  28. transitions[0].StorageClass = obs.StorageClassWarm
  29.  
  30. transitions[1] = obs.Transition{}
  31. transitions[1].Days = 60
  32. transitions[1].StorageClass = obs.StorageClassCold
  33. lifecycleRule0.Transitions = transitions[:]
  34.  
  35. lifecycleRules[0] = lifecycleRule0
  36.  
  37. lifecycleRule1 := obs.LifecycleRule{}
  38. lifecycleRule1.Status = obs.RuleStatusEnabled
  39. lifecycleRule1.ID = "rule1"
  40. lifecycleRule1.Prefix = "prefix1"
  41.  
  42. // 指定满足前缀的对象成为历史版本30天后转换为低频访问存储,指定满足前缀的对象成为历史版本60天后转换为归档存储
  43.  
  44. var noncurrentTransitions [2]obs.NoncurrentVersionTransition
  45. noncurrentTransitions[0] = obs.NoncurrentVersionTransition{}
  46. noncurrentTransitions[0].NoncurrentDays = 30
  47. noncurrentTransitions[0].StorageClass = obs.StorageClassWarm
  48.  
  49. noncurrentTransitions[1] = obs.NoncurrentVersionTransition{}
  50. noncurrentTransitions[1].NoncurrentDays = 60
  51. noncurrentTransitions[1].StorageClass = obs.StorageClassCold
  52. lifecycleRule1.NoncurrentVersionTransitions = noncurrentTransitions[:]
  53. lifecycleRules[1] = lifecycleRule1
  54.  
  55. input.LifecycleRules = lifecycleRules[:]
  56. output, err := obsClient.SetBucketLifecycleConfiguration(input)
  57. if err == nil {
  58. fmt.Printf("RequestId:%s\n", output.RequestId)
  59. } else if obsError, ok := err.(obs.ObsError); ok {
  60. fmt.Printf("Code:%s\n", obsError.Code)
  61. fmt.Printf("Message:%s\n", obsError.Message)
  62. }
  63. }

设置对象过期时间

以下代码展示了如何设置最新版本对象和历史版本对象的过期时间:

  1. // 引入依赖包
  2. import (
  3. "fmt"
  4. "obs"
  5. "time"
  6. )
  7.  
  8. var ak = "*** Provide your Access Key ***"
  9. var sk = "*** Provide your Secret Key ***"
  10. var endpoint = "https://your-endpoint"
  11.  
  12. // 创建ObsClient结构体
  13. var obsClient, _ = obs.New(ak, sk, endpoint)
  14.  
  15. func main() {
  16. input := &obs.SetBucketLifecycleConfigurationInput{}
  17. input.Bucket = "bucketname"
  18. var lifecycleRules [2]obs.LifecycleRule
  19. lifecycleRule0 := obs.LifecycleRule{}
  20. lifecycleRule0.ID = "rule0"
  21. lifecycleRule0.Prefix = "prefix0"
  22. lifecycleRule0.Status = obs.RuleStatusEnabled
  23. // 指定满足前缀的对象创建30天后过期,指定满足前缀的对象成为历史版本30天后过期
  24. lifecycleRule0.Expiration.Days = 30
  25. lifecycleRule0.NoncurrentVersionExpiration.NoncurrentDays = 30
  26.  
  27. lifecycleRules[0] = lifecycleRule0
  28.  
  29. lifecycleRule1 := obs.LifecycleRule{}
  30. lifecycleRule1.Status = obs.RuleStatusEnabled
  31. lifecycleRule1.ID = "rule1"
  32. lifecycleRule1.Prefix = "prefix1"
  33. // 直接指定满足前缀的对象过期时间
  34. lifecycleRule1.Expiration.Date = time.Now().Add(time.Duration(24*60) * time.Hour)
  35. lifecycleRules[1] = lifecycleRule1
  36.  
  37. input.LifecycleRules = lifecycleRules[:]
  38. output, err := obsClient.SetBucketLifecycleConfiguration(input)
  39. if err == nil {
  40. fmt.Printf("RequestId:%s\n", output.RequestId)
  41. } else if obsError, ok := err.(obs.ObsError); ok {
  42. fmt.Printf("Code:%s\n", obsError.Code)
  43. fmt.Printf("Message:%s\n", obsError.Message)
  44. }
  45. }

父主题:生命周期管理