OSDN Git Service

Create ossClient.go (#574)
[bytom/vapor.git] / vendor / github.com / aliyun / aliyun-oss-go-sdk / sample / bucket_lifecycle.go
1 package sample
2
3 import (
4         "fmt"
5
6         "github.com/aliyun/aliyun-oss-go-sdk/oss"
7 )
8
9 // BucketLifecycleSample shows how to set, get and delete bucket's lifecycle.
10 func BucketLifecycleSample() {
11         // New client
12         client, err := oss.New(endpoint, accessID, accessKey)
13         if err != nil {
14                 HandleError(err)
15         }
16
17         // Create the bucket with default parameters
18         err = client.CreateBucket(bucketName)
19         if err != nil {
20                 HandleError(err)
21         }
22
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",
26         }
27         rule1 := oss.LifecycleRule{
28                 ID:         "rule1",
29                 Prefix:     "one",
30                 Status:     "Enabled",
31                 Expiration: &expriation,
32         }
33         var rules = []oss.LifecycleRule{rule1}
34         err = client.SetBucketLifecycle(bucketName, rules)
35         if err != nil {
36                 HandleError(err)
37         }
38
39         // Case 2: Get the bucket's lifecycle
40         lc, err := client.GetBucketLifecycle(bucketName)
41         if err != nil {
42                 HandleError(err)
43         }
44         fmt.Printf("Bucket Lifecycle:%v, %v\n", lc.Rules, *lc.Rules[0].Expiration)
45
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{
48                 Days:         3,
49                 StorageClass: oss.StorageIA,
50         }
51         transitionArch := oss.LifecycleTransition{
52                 Days:         30,
53                 StorageClass: oss.StorageArchive,
54         }
55         rule2 := oss.LifecycleRule{
56                 ID:          "rule2",
57                 Prefix:      "two",
58                 Status:      "Enabled",
59                 Transitions: []oss.LifecycleTransition{transitionIA, transitionArch},
60         }
61         rules = []oss.LifecycleRule{rule2}
62         err = client.SetBucketLifecycle(bucketName, rules)
63         if err != nil {
64                 HandleError(err)
65         }
66
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{
69                 Days: 3,
70         }
71         rule3 := oss.LifecycleRule{
72                 ID:                   "rule3",
73                 Prefix:               "three",
74                 Status:               "Enabled",
75                 AbortMultipartUpload: &abortMPU,
76         }
77         rules = append(lc.Rules, rule3)
78         err = client.SetBucketLifecycle(bucketName, rules)
79         if err != nil {
80                 HandleError(err)
81         }
82
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",
86         }
87         tag1 := oss.Tag{
88                 Key:   "key1",
89                 Value: "value1",
90         }
91         tag2 := oss.Tag{
92                 Key:   "key2",
93                 Value: "value2",
94         }
95         rule4 := oss.LifecycleRule{
96                 ID:         "rule4",
97                 Prefix:     "four",
98                 Status:     "Enabled",
99                 Tags:       []oss.Tag{tag1, tag2},
100                 Expiration: &expriation,
101         }
102         rules = []oss.LifecycleRule{rule4}
103         err = client.SetBucketLifecycle(bucketName, rules)
104         if err != nil {
105                 HandleError(err)
106         }
107
108         // Case 6: Delete bucket's Lifecycle
109         err = client.DeleteBucketLifecycle(bucketName)
110         if err != nil {
111                 HandleError(err)
112         }
113
114         // Delete bucket
115         err = client.DeleteBucket(bucketName)
116         if err != nil {
117                 HandleError(err)
118         }
119
120         fmt.Println("BucketLifecycleSample completed")
121 }