bucket_lifecycle.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 id1 and the applied objects' prefix is one and expired time is 11/11/2015
  19. //var rule1 = oss.BuildLifecycleRuleByDate("id1", "one", true, 2015, 11, 11)
  20. rule1, err := oss.NewLifecycleRuleByCreateBeforeDate("id1", "one", true, 2015, 11, 11, oss.LRTExpriration)
  21. if err != nil {
  22. HandleError(err)
  23. }
  24. var rules = []oss.LifecycleRule{*rule1}
  25. err = client.SetBucketLifecycle(bucketName, rules)
  26. if err != nil {
  27. HandleError(err)
  28. }
  29. // 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.
  30. //var rule2 = oss.BuildLifecycleRuleByDays("id2", "two", true, 3)
  31. rule2, err := oss.NewLifecleRuleByDays("id2", "two", true, 3, oss.LRTTransition, oss.StorageIA)
  32. if err != nil {
  33. HandleError(err)
  34. }
  35. rules = []oss.LifecycleRule{*rule2}
  36. err = client.SetBucketLifecycle(bucketName, rules)
  37. if err != nil {
  38. HandleError(err)
  39. }
  40. // Get the bucket's lifecycle
  41. lc, err := client.GetBucketLifecycle(bucketName)
  42. if err != nil {
  43. HandleError(err)
  44. }
  45. fmt.Println("Bucket Lifecycle:", lc.Rules)
  46. rule3, err := oss.NewLifecleRuleByDays("id3", "three", true, 3, oss.LRTAbortMultiPartUpload)
  47. if err != nil {
  48. HandleError(err)
  49. }
  50. rules = append(lc.Rules, *rule3)
  51. err = client.SetBucketLifecycle(bucketName, rules)
  52. if err != nil {
  53. HandleError(err)
  54. }
  55. // Get the bucket's lifecycle
  56. lc, err = client.GetBucketLifecycle(bucketName)
  57. if err != nil {
  58. HandleError(err)
  59. }
  60. fmt.Println("Bucket Lifecycle:", lc.Rules)
  61. // Delete bucket's Lifecycle
  62. err = client.DeleteBucketLifecycle(bucketName)
  63. if err != nil {
  64. HandleError(err)
  65. }
  66. // Delete bucket
  67. err = client.DeleteBucket(bucketName)
  68. if err != nil {
  69. HandleError(err)
  70. }
  71. fmt.Println("BucketLifecycleSample completed")
  72. }