bucket_lifecycle.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package sample
  2. import (
  3. "fmt"
  4. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  5. )
  6. // BucketLifecycleSample shows how to set, get and delete bucket's lifecycle.
  7. func BucketLifecycleSample() {
  8. // New client
  9. client, err := oss.New(endpoint, accessID, accessKey)
  10. if err != nil {
  11. HandleError(err)
  12. }
  13. // Create the bucket with default parameters
  14. err = client.CreateBucket(bucketName)
  15. if err != nil {
  16. HandleError(err)
  17. }
  18. // Case 1: Set the lifecycle. The rule ID is rule1 and the applied objects' prefix is one and expired time is 11/11/2015
  19. expriation := oss.LifecycleExpiration{
  20. CreatedBeforeDate: "2015-11-11T00:00:00.000Z",
  21. }
  22. rule1 := oss.LifecycleRule{
  23. ID: "rule1",
  24. Prefix: "one",
  25. Status: "Enabled",
  26. Expiration: &expriation,
  27. }
  28. var rules = []oss.LifecycleRule{rule1}
  29. err = client.SetBucketLifecycle(bucketName, rules)
  30. if err != nil {
  31. HandleError(err)
  32. }
  33. // Get the bucket's lifecycle
  34. lc, err := client.GetBucketLifecycle(bucketName)
  35. if err != nil {
  36. HandleError(err)
  37. }
  38. fmt.Printf("Bucket Lifecycle:%v, %v\n", lc.Rules, *lc.Rules[0].Expiration)
  39. // Case 2: Set the lifecycle, The rule ID is id2 and the applied objects' prefix is two and the expired time is three days after the object created.
  40. transitionIA := oss.LifecycleTransition{
  41. Days: 3,
  42. StorageClass: oss.StorageIA,
  43. }
  44. transitionArch := oss.LifecycleTransition{
  45. Days: 30,
  46. StorageClass: oss.StorageArchive,
  47. }
  48. rule2 := oss.LifecycleRule{
  49. ID: "rule2",
  50. Prefix: "two",
  51. Status: "Enabled",
  52. Transitions: []oss.LifecycleTransition{transitionIA, transitionArch},
  53. }
  54. rules = []oss.LifecycleRule{rule2}
  55. err = client.SetBucketLifecycle(bucketName, rules)
  56. if err != nil {
  57. HandleError(err)
  58. }
  59. // Get the bucket's lifecycle
  60. lc, err = client.GetBucketLifecycle(bucketName)
  61. if err != nil {
  62. HandleError(err)
  63. }
  64. fmt.Printf("Bucket Lifecycle:%v\n", lc.Rules)
  65. abortMPU := oss.LifecycleAbortMultipartUpload{
  66. Days: 3,
  67. }
  68. rule3 := oss.LifecycleRule{
  69. ID: "rule3",
  70. Prefix: "three",
  71. Status: "Enabled",
  72. AbortMultipartUpload: &abortMPU,
  73. }
  74. rules = append(lc.Rules, rule3)
  75. err = client.SetBucketLifecycle(bucketName, rules)
  76. if err != nil {
  77. HandleError(err)
  78. }
  79. // Get the bucket's lifecycle
  80. lc, err = client.GetBucketLifecycle(bucketName)
  81. if err != nil {
  82. HandleError(err)
  83. }
  84. fmt.Printf("Bucket Lifecycle:%v, %v\n", lc.Rules, *lc.Rules[1].AbortMultipartUpload)
  85. // Delete bucket's Lifecycle
  86. err = client.DeleteBucketLifecycle(bucketName)
  87. if err != nil {
  88. HandleError(err)
  89. }
  90. // Delete bucket
  91. err = client.DeleteBucket(bucketName)
  92. if err != nil {
  93. HandleError(err)
  94. }
  95. fmt.Println("BucketLifecycleSample completed")
  96. }