-
Notifications
You must be signed in to change notification settings - Fork 231
/
comm.go
181 lines (157 loc) · 3.54 KB
/
comm.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package sample
import (
"fmt"
"os"
"strings"
"time"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
var (
pastDate = time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
futureDate = time.Date(2049, time.January, 10, 23, 0, 0, 0, time.UTC)
)
// HandleError is the error handling method in the sample code
func HandleError(err error) {
fmt.Println("occurred error:", err)
os.Exit(-1)
}
// GetTestBucket creates the test bucket
func GetTestBucket(bucketName string) (*oss.Bucket, error) {
// New client
client, err := oss.New(endpoint, accessID, accessKey)
if err != nil {
return nil, err
}
// Create bucket
err = client.CreateBucket(bucketName)
if err != nil {
return nil, err
}
// Get bucket
bucket, err := client.Bucket(bucketName)
if err != nil {
return nil, err
}
return bucket, nil
}
// DeleteTestBucketAndLiveChannel 删除sample的channelname和bucket,该函数为了简化sample,让sample代码更明了
func DeleteTestBucketAndLiveChannel(bucketName string) error {
// New Client
client, err := oss.New(endpoint, accessID, accessKey)
if err != nil {
return err
}
// Get Bucket
bucket, err := client.Bucket(bucketName)
if err != nil {
return err
}
marker := ""
for {
result, err := bucket.ListLiveChannel(oss.Marker(marker))
if err != nil {
HandleError(err)
}
for _, channel := range result.LiveChannel {
err := bucket.DeleteLiveChannel(channel.Name)
if err != nil {
HandleError(err)
}
}
if result.IsTruncated {
marker = result.NextMarker
} else {
break
}
}
// Delete Bucket
err = client.DeleteBucket(bucketName)
if err != nil {
return err
}
return nil
}
// DeleteTestBucketAndObject deletes the test bucket and its objects
func DeleteTestBucketAndObject(bucketName string) error {
// New client
client, err := oss.New(endpoint, accessID, accessKey)
if err != nil {
return err
}
// Get bucket
bucket, err := client.Bucket(bucketName)
if err != nil {
return err
}
// Delete part
keyMarker := oss.KeyMarker("")
uploadIDMarker := oss.UploadIDMarker("")
for {
lmur, err := bucket.ListMultipartUploads(keyMarker, uploadIDMarker)
if err != nil {
return err
}
for _, upload := range lmur.Uploads {
var imur = oss.InitiateMultipartUploadResult{Bucket: bucket.BucketName,
Key: upload.Key, UploadID: upload.UploadID}
err = bucket.AbortMultipartUpload(imur)
if err != nil {
return err
}
}
keyMarker = oss.KeyMarker(lmur.NextKeyMarker)
uploadIDMarker = oss.UploadIDMarker(lmur.NextUploadIDMarker)
if !lmur.IsTruncated {
break
}
}
// Delete objects
marker := oss.Marker("")
for {
lor, err := bucket.ListObjects(marker)
if err != nil {
return err
}
for _, object := range lor.Objects {
err = bucket.DeleteObject(object.Key)
if err != nil {
return err
}
}
marker = oss.Marker(lor.NextMarker)
if !lor.IsTruncated {
break
}
}
// Delete bucket
err = client.DeleteBucket(bucketName)
if err != nil {
return err
}
return nil
}
// Object defines pair of key and value
type Object struct {
Key string
Value string
}
// CreateObjects creates some objects
func CreateObjects(bucket *oss.Bucket, objects []Object) error {
for _, object := range objects {
err := bucket.PutObject(object.Key, strings.NewReader(object.Value))
if err != nil {
return err
}
}
return nil
}
// DeleteObjects deletes some objects.
func DeleteObjects(bucket *oss.Bucket, objects []Object) error {
for _, object := range objects {
err := bucket.DeleteObject(object.Key)
if err != nil {
return err
}
}
return nil
}