6 "github.com/aliyun/aliyun-oss-go-sdk/oss"
9 // BucketLifecycleSample shows how to set, get and delete bucket's lifecycle.
10 func BucketLifecycleSample() {
12 client, err := oss.New(endpoint, accessID, accessKey)
17 // Create the bucket with default parameters
18 err = client.CreateBucket(bucketName)
23 // 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
24 expriation := oss.LifecycleExpiration{
25 CreatedBeforeDate: "2015-11-11T00:00:00.000Z",
27 rule1 := oss.LifecycleRule{
31 Expiration: &expriation,
33 var rules = []oss.LifecycleRule{rule1}
34 err = client.SetBucketLifecycle(bucketName, rules)
39 // Case 2: Get the bucket's lifecycle
40 lc, err := client.GetBucketLifecycle(bucketName)
44 fmt.Printf("Bucket Lifecycle:%v, %v\n", lc.Rules, *lc.Rules[0].Expiration)
46 // 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
47 transitionIA := oss.LifecycleTransition{
49 StorageClass: oss.StorageIA,
51 transitionArch := oss.LifecycleTransition{
53 StorageClass: oss.StorageArchive,
55 rule2 := oss.LifecycleRule{
59 Transitions: []oss.LifecycleTransition{transitionIA, transitionArch},
61 rules = []oss.LifecycleRule{rule2}
62 err = client.SetBucketLifecycle(bucketName, rules)
67 // 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.
68 abortMPU := oss.LifecycleAbortMultipartUpload{
71 rule3 := oss.LifecycleRule{
75 AbortMultipartUpload: &abortMPU,
77 rules = append(lc.Rules, rule3)
78 err = client.SetBucketLifecycle(bucketName, rules)
83 // 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
84 expriation = oss.LifecycleExpiration{
85 CreatedBeforeDate: "2015-11-11T00:00:00.000Z",
95 rule4 := oss.LifecycleRule{
99 Tags: []oss.Tag{tag1, tag2},
100 Expiration: &expriation,
102 rules = []oss.LifecycleRule{rule4}
103 err = client.SetBucketLifecycle(bucketName, rules)
108 // Case 6: Delete bucket's Lifecycle
109 err = client.DeleteBucketLifecycle(bucketName)
115 err = client.DeleteBucket(bucketName)
120 fmt.Println("BucketLifecycleSample completed")