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