-
Notifications
You must be signed in to change notification settings - Fork 5
/
lifecycle.go
116 lines (106 loc) · 3.57 KB
/
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
package tos
import (
"bytes"
"context"
"net/http"
"time"
)
func (cli *ClientV2) parseLifecycleInput(input *PutBucketLifecycleInput) putBucketLifecycleInput {
lifecycleInput := make([]lifecycleRule, 0, len(input.Rules))
for _, lifecycle := range input.Rules {
var exp *expiration
if lifecycle.Expiration != nil {
exp = &expiration{
Days: lifecycle.Expiration.Days,
}
if !lifecycle.Expiration.Date.IsZero() {
exp.Date = lifecycle.Expiration.Date.Format(time.RFC3339)
}
}
transitionList := make([]transition, 0, len(lifecycle.Transitions))
for _, trans := range lifecycle.Transitions {
t := transition{
Days: trans.Days,
StorageClass: trans.StorageClass,
}
if !trans.Date.IsZero() {
t.Date = trans.Date.Format(time.RFC3339)
}
transitionList = append(transitionList, t)
}
lifecycleInput = append(lifecycleInput, lifecycleRule{
ID: lifecycle.ID,
Prefix: lifecycle.Prefix,
Status: lifecycle.Status,
Transitions: transitionList,
Expiration: exp,
NonCurrentVersionTransition: lifecycle.NonCurrentVersionTransition,
NoCurrentVersionExpiration: lifecycle.NoCurrentVersionExpiration,
Tag: lifecycle.Tag,
AbortInCompleteMultipartUpload: lifecycle.AbortInCompleteMultipartUpload,
})
}
return putBucketLifecycleInput{Rules: lifecycleInput}
}
func (cli *ClientV2) PutBucketLifecycle(ctx context.Context, input *PutBucketLifecycleInput) (*PutLifecycleOutput, error) {
if input == nil {
return nil, InputIsNilClientError
}
if err := isValidBucketName(input.Bucket, cli.isCustomDomain); err != nil {
return nil, err
}
data, contentMD5, err := marshalInput("PutBucketLifecycleInput", cli.parseLifecycleInput(input))
if err != nil {
return nil, err
}
res, err := cli.newBuilder(input.Bucket, "").
WithQuery("lifecycle", "").
WithHeader(HeaderContentMD5, contentMD5).
WithRetry(OnRetryFromStart, StatusCodeClassifier{}).
Request(ctx, http.MethodPut, bytes.NewReader(data), cli.roundTripper(http.StatusOK))
if err != nil {
return nil, err
}
defer res.Close()
output := PutLifecycleOutput{RequestInfo: res.RequestInfo()}
return &output, nil
}
func (cli *ClientV2) GetBucketLifecycle(ctx context.Context, input *GetBucketLifecycleInput) (*GetBucketLifecycleOutput, error) {
if input == nil {
return nil, InputIsNilClientError
}
if err := isValidBucketName(input.Bucket, cli.isCustomDomain); err != nil {
return nil, err
}
res, err := cli.newBuilder(input.Bucket, "").
WithQuery("lifecycle", "").
WithRetry(nil, StatusCodeClassifier{}).
Request(ctx, http.MethodGet, nil, cli.roundTripper(http.StatusOK))
if err != nil {
return nil, err
}
defer res.Close()
output := GetBucketLifecycleOutput{RequestInfo: res.RequestInfo()}
if err = marshalOutput(res, &output); err != nil {
return nil, err
}
return &output, nil
}
func (cli *ClientV2) DeleteBucketLifecycle(ctx context.Context, input *DeleteBucketLifecycleInput) (*DeleteBucketLifecycleOutput, error) {
if input == nil {
return nil, InputIsNilClientError
}
if err := isValidBucketName(input.Bucket, cli.isCustomDomain); err != nil {
return nil, err
}
res, err := cli.newBuilder(input.Bucket, "").
WithQuery("lifecycle", "").
WithRetry(nil, StatusCodeClassifier{}).
Request(ctx, http.MethodDelete, nil, cli.roundTripper(http.StatusNoContent))
if err != nil {
return nil, err
}
defer res.Close()
output := DeleteBucketLifecycleOutput{RequestInfo: res.RequestInfo()}
return &output, nil
}