bucket_lifecycle.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 the last modified Date is before 2015/11/11
  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. // Case 2: 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 3: Set the lifecycle, The rule ID is rule2 and the applied objects' prefix is two. The object start with the prefix will be transited to IA storage Type 3 days latter, and to archive storage type 30 days latter
  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. // Case 4: Set the lifecycle, The rule ID is rule3 and the applied objects' prefix is three. The object start with the prefix will be transited to IA storage Type 3 days latter, and to archive storage type 30 days latter, the uncompleted multipart upload will be abort 3 days latter.
  60. abortMPU := oss.LifecycleAbortMultipartUpload{
  61. Days: 3,
  62. }
  63. rule3 := oss.LifecycleRule{
  64. ID: "rule3",
  65. Prefix: "three",
  66. Status: "Enabled",
  67. AbortMultipartUpload: &abortMPU,
  68. }
  69. rules = append(lc.Rules, rule3)
  70. err = client.SetBucketLifecycle(bucketName, rules)
  71. if err != nil {
  72. HandleError(err)
  73. }
  74. // Case 5: Set the lifecycle. The rule ID is rule4 and the applied objects' has the tagging which prefix is four and the last modified Date is before 2015/11/11
  75. expriation = oss.LifecycleExpiration{
  76. CreatedBeforeDate: "2015-11-11T00:00:00.000Z",
  77. }
  78. tag1 := oss.Tag{
  79. Key: "key1",
  80. Value: "value1",
  81. }
  82. tag2 := oss.Tag{
  83. Key: "key2",
  84. Value: "value2",
  85. }
  86. rule4 := oss.LifecycleRule{
  87. ID: "rule4",
  88. Prefix: "four",
  89. Status: "Enabled",
  90. Tags: []oss.Tag{tag1, tag2},
  91. Expiration: &expriation,
  92. }
  93. rules = []oss.LifecycleRule{rule4}
  94. err = client.SetBucketLifecycle(bucketName, rules)
  95. if err != nil {
  96. HandleError(err)
  97. }
  98. // Case 6: Delete bucket's Lifecycle
  99. err = client.DeleteBucketLifecycle(bucketName)
  100. if err != nil {
  101. HandleError(err)
  102. }
  103. // Delete bucket
  104. err = client.DeleteBucket(bucketName)
  105. if err != nil {
  106. HandleError(err)
  107. }
  108. fmt.Println("BucketLifecycleSample completed")
  109. }