-
Notifications
You must be signed in to change notification settings - Fork 99
/
bucket_replication.go
352 lines (295 loc) · 12.2 KB
/
bucket_replication.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package lib
import (
"fmt"
"io/ioutil"
"os"
"strings"
)
var specChineseReplication = SpecText{
synopsisText: "bucket的跨区域同步配置管理",
paramText: "bucket_url [local_xml_file|ruleID] [options]",
syntaxText: `
ossuitl replication --method put oss://bucket local_xml_file [options]
ossuitl replication --method get oss://bucket [options]
ossuitl replication --method delete oss://bucket ruleID [options]
ossuitl replication --method get --item location oss://bucket [options]
ossuitl replication --method get --item progress oss://bucket [ruleID] [options]
`,
detailHelpText: `
replication命令通过设置method选项值为put、get、delete,可以设置、查询或者删除bucket的跨区域复制规则;
此外,当method选项为get时,可通过设置item选项选项值为location、progress,可以查询可复制到的目标bucket
所在的地域或者bucket的跨区域复制进度信息
用法:
该命令有五种用法:
1) ossuitl replication --method put oss://bucket local_xml_file [options]
这个命令从配置文件local_xml_file中读取跨区域复制的配置,然后设置bucket的跨区域复制规则,
配置文件是一个xml格式的文件,举例如下
<?xml version="1.0" encoding="UTF-8"?>
<ReplicationConfiguration>
<Rule>
<PrefixSet>
<Prefix>prefix_1</Prefix>
<Prefix>prefix_2</Prefix>
</PrefixSet>
<Action>ALL,PUT</Action>
<Destination>
<Bucket>dest-bucket-name</Bucket>
<Location>oss-cn-hangzhou</Location>
<TransferType>oss_acc</TransferType>
</Destination>
<HistoricalObjectReplication>enabled</HistoricalObjectReplication>
</Rule>
</ReplicationConfiguration>
2) ossuitl replication --method get oss://bucket [options]
这个命令查询bucket的跨区域复制规则,目前只支持输出到屏幕上
3) ossuitl replication --method delete oss://bucket ruleId [options]
这个命令停止bucket的跨区域复制并删除bucket的复制配置
4) ossuitl replication --method get --item location oss://bucket [options]
这个命令查询可复制到的目标bucket所在的地域,目前只支持输出到屏幕上
5) ossuitl replication --method get --item progress oss://bucket [ruleID] [options]
这个命令查询bucket的跨区域复制进度,如果输入参数ruleID,则查询该ID对应的跨区域复制进度,否则查询bucket所有的跨区域复制进度,目前只支持输出到屏幕上
`,
sampleText: `
1) 设置bucket的跨区域复制规则
ossuitl replication --method put oss://bucket local_xml_file
2) 查询bucket的跨区域复制规则,结果输出到标准输出
ossuitl replication --method get oss://bucket
3) 删除bucket的跨区域复制规则
ossuitl replication --method delete oss://bucket ruleID
4) 查询可复制到的目标bucket所在的地域,结果输出到标准输出
ossuitl replication --method get --item location oss://bucket
5) 查询bucket所有的跨区域复制进度,结果输出到标准输出
ossuitl replication --method get --item progress oss://bucket
6) 查询bucket在指定ruleID下的跨区域复制进度,结果输出到标准输出
ossuitl replication --method get --item progress oss://bucket ruleID
`,
}
var specEnglishreplication = SpecText{
synopsisText: "manage bucket's replication configuration",
paramText: "bucket_url [local_xml_file|ruleID] [options]",
syntaxText: `
ossuitl replication --method put oss://bucket local_xml_file [options]
ossuitl replication --method get oss://bucket [options]
ossuitl replication --method delete oss://bucket ruleID [options]
ossuitl replication --method get --item location oss://bucket [options]
ossuitl replication --method get --item progress oss://bucket [ruleID] [options]
`,
detailHelpText: `
replication command can set, get and delete cross region replication rules of
the oss bucket by setting method option value to put, get and delete; in addition,
when the method option is get, you can get the region where the target bucket can
be copied to or the cross region replication progress of the bucket by setting item
option value to location and progress
Usage:
There are five usages for this command:
1) ossuitl replication --method put oss://bucket local_xml_file [options]
The command sets the cross region replication rules of bucket from local file local_xml_file
The local_xml_file is xml format,for example
<?xml version="1.0" encoding="UTF-8"?>
<ReplicationConfiguration>
<Rule>
<PrefixSet>
<Prefix>prefix_1</Prefix>
<Prefix>prefix_2</Prefix>
</PrefixSet>
<Action>ALL,PUT</Action>
<Destination>
<Bucket>dest-bucket-name</Bucket>
<Location>oss-cn-hangzhou</Location>
<TransferType>oss_acc</TransferType>
</Destination>
<HistoricalObjectReplication>enabled</HistoricalObjectReplication>
</Rule>
</ReplicationConfiguration>
2) ossuitl replication --method get oss://bucket [options]
The command gets the cross region replication rules of bucket
At present, it only supports output to stdout
3) ossuitl replication --method delete oss://bucket ruleID
The command stops the cross region replication of bucket and
removes the replication configuration of bucket
4) ossuitl replication --method get --item location oss://bucket [options]
The command gets the regions of the target bucket that can be copied to
At present, it only supports output to stdout
5) ossuitl replication --method get --item progress oss://bucket [ruleID] [options]
The command gets the cross region replication progress of bucket
If you input the parameter ruleID, the cross region replication progress corresponding to the ID will be gotten
If you don't input the parameter ruleID, all cross region replication progress will be gotten
At present, it only supports output to stdout
`,
sampleText: `
1) put bucket cross region replication rules
ossuitl replication --method put oss://bucket local_xml_file
2) get bucket cross region replication rules, output to stdout
ossuitl replication --method get oss://bucket
3) delete bucket cross region replication rules
ossuitl replication --method delete oss://bucket ruleID
4) get regions where the target bucket can be copied to, output to stdout
ossuitl replication --method get --item location oss://bucket
5) get bucket all the cross region replication progress, output to stdout
ossuitl replication --method get --item progress oss://bucket
6) get bucket the cross region replication progress with the specified ruleID, output to stdout
ossuitl replication --method get --item progress oss://bucket ruleID
`,
}
type ReplicationCommand struct {
command Command
bucketName string
}
var replicationCommand = ReplicationCommand{
command: Command{
name: "replication",
nameAlias: []string{"replication"},
minArgc: 1,
maxArgc: 2,
specChinese: specChineseReplication,
specEnglish: specEnglishreplication,
group: GroupTypeNormalCommand,
validOptionNames: []string{
OptionConfigFile,
OptionEndpoint,
OptionAccessKeyID,
OptionAccessKeySecret,
OptionSTSToken,
OptionProxyHost,
OptionProxyUser,
OptionProxyPwd,
OptionLogLevel,
OptionMode,
OptionECSRoleName,
OptionTokenTimeout,
OptionRamRoleArn,
OptionRoleSessionName,
OptionReadTimeout,
OptionConnectTimeout,
OptionSTSRegion,
OptionMethod,
OptionItem,
OptionSkipVerfiyCert,
},
},
}
// function for FormatHelper interface
func (replicationc *ReplicationCommand) formatHelpForWhole() string {
return replicationc.command.formatHelpForWhole()
}
func (replicationc *ReplicationCommand) formatIndependHelp() string {
return replicationc.command.formatIndependHelp()
}
// Init simulate inheritance, and polymorphism
func (replicationc *ReplicationCommand) Init(args []string, options OptionMapType) error {
return replicationc.command.Init(args, options, replicationc)
}
// RunCommand simulate inheritance, and polymorphism
func (replicationc *ReplicationCommand) RunCommand() error {
strMethod, _ := GetString(OptionMethod, replicationc.command.options)
strItem, _ := GetString(OptionItem, replicationc.command.options)
if strMethod == "" {
return fmt.Errorf("--method value is empty")
}
strMethod = strings.ToLower(strMethod)
if strMethod != "put" && strMethod != "get" && strMethod != "delete" {
return fmt.Errorf("--method value is not in the optional value:put|get|delete")
}
strItem = strings.ToLower(strItem)
if strMethod == "get" && strItem != "" && strItem != "location" && strItem != "progress" {
return fmt.Errorf("--item value is not in the optional value:location|progress")
}
srcBucketUrL, err := GetCloudUrl(replicationc.command.args[0], "")
if err != nil {
return err
}
replicationc.bucketName = srcBucketUrL.bucket
if strMethod == "put" {
err = replicationc.PutBucketReplication()
} else if strMethod == "get" {
if strItem == "" {
err = replicationc.GetBucketReplication()
} else if strItem == "location" {
err = replicationc.GetBucketReplicationLocation()
} else if strItem == "progress" {
err = replicationc.GetBucketReplicationProgress()
}
} else if strMethod == "delete" {
err = replicationc.DeleteBucketReplication()
}
return err
}
func (replicationc *ReplicationCommand) PutBucketReplication() error {
if len(replicationc.command.args) < 2 {
return fmt.Errorf("put bucket replication need at least 2 parameters,the local xml file is empty")
}
xmlFile := replicationc.command.args[1]
fileInfo, err := os.Stat(xmlFile)
if err != nil {
return err
}
if fileInfo.IsDir() {
return fmt.Errorf("%s is dir,not the expected file", xmlFile)
}
if fileInfo.Size() == 0 {
return fmt.Errorf("%s is empty file", xmlFile)
}
// parsing the xml file
file, err := os.Open(xmlFile)
if err != nil {
return err
}
defer file.Close()
text, err := ioutil.ReadAll(file)
if err != nil {
return err
}
client, err := replicationc.command.ossClient(replicationc.bucketName)
if err != nil {
return err
}
return client.PutBucketReplication(replicationc.bucketName, string(text))
}
func (replicationc *ReplicationCommand) GetBucketReplication() error {
client, err := replicationc.command.ossClient(replicationc.bucketName)
if err != nil {
return err
}
data, err := client.GetBucketReplication(replicationc.bucketName)
if err == nil {
fmt.Printf("%s\n", data)
}
return err
}
func (replicationc *ReplicationCommand) DeleteBucketReplication() error {
if len(replicationc.command.args) < 2 {
return fmt.Errorf("delete bucket replication need at least 2 parameters,the rule ID is empty")
}
ruleID := replicationc.command.args[1]
client, err := replicationc.command.ossClient(replicationc.bucketName)
if err != nil {
return err
}
return client.DeleteBucketReplication(replicationc.bucketName, ruleID)
}
func (replicationc *ReplicationCommand) GetBucketReplicationLocation() error {
client, err := replicationc.command.ossClient(replicationc.bucketName)
if err != nil {
return err
}
data, err := client.GetBucketReplicationLocation(replicationc.bucketName)
if err == nil {
fmt.Printf("%s\n", data)
}
return err
}
func (replicationc *ReplicationCommand) GetBucketReplicationProgress() error {
ruleID := ""
if len(replicationc.command.args) >= 2 {
ruleID = replicationc.command.args[1]
}
client, err := replicationc.command.ossClient(replicationc.bucketName)
if err != nil {
return err
}
data, err := client.GetBucketReplicationProgress(replicationc.bucketName, ruleID)
if err == nil {
fmt.Printf("%s\n", data)
}
return err
}