-
Notifications
You must be signed in to change notification settings - Fork 100
/
object_tagging.go
369 lines (313 loc) · 11.7 KB
/
object_tagging.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package lib
import (
"fmt"
"strings"
"sync"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
var specChineseObjectTag = SpecText{
synopsisText: "设置、查询或者删除object的tag配置",
paramText: "cloud_url [tag_parameter] [options]",
syntaxText: `
ossutil object-tagging --method put oss://bucket[/prefix] key#value [--encoding-type url] [-r] [--payer requester] [--version-id versionId] [-c file]
ossutil object-tagging --method get oss://bucket[/prefix] [--encoding-type url] [-r] [--payer requester] [--version-id versionId] [-c file]
ossuitl object-tagging --method delete oss://bucket[/prefix] [--encoding-type url] [-r] [--payer requester] [--version-id versionId] [-c file]
`,
detailHelpText: `
object-tagging命令通过设置method选项值为put、get、delete,可以设置、查询或者删除object的tag配置
每个tag的key和value必须以字符'#'分隔,最多可以连续输入10个tag信息
用法:
该命令有三种用法:
1) ossutil object-tagging --method put oss://bucket/object tagkey#tagvalue
这个命令设置object的tag配置,key和value分别为tagkey、tagvalue
2) ossutil object-tagging --method get oss://bucket/object
这个命令查询object的tag配置
3) ossutil object-tagging --method delete oss://bucket/object
这个命令删除object的tag配置
`,
sampleText: `
1) 设置object的tag配置
ossutil object-tagging --method put oss://bucket/object tagkey#tagvalue
2) 批量设置objects的tag配置
ossutil object-tagging --method put oss://bucket/prefix -r tagkey#tagvalue
3) 设置object的多个tag配置
ossutil object-tagging --method put oss://bucket/object tagkey1#tagvalue1 tagkey2#tagvalue2
4) 查询object的tag配置
ossutil object-tagging --method get oss://bucket/object
5) 批量查询object的tag配置
ossutil object-tagging --method get oss://bucket/prefix -r
6) 删除object的tag配置
ossutil object-tagging --method delete oss://bucket/object
7) 批量删除object的tag配置
ossutil object-tagging --method delete oss://bucket/prefix -r
`,
}
var specEnglishObjectTag = SpecText{
synopsisText: "Set, get or delete object tag configuration",
paramText: "cloud_url [tag_parameter] [options]",
syntaxText: `
ossutil object-tagging --method put oss://bucket[/prefix] key#value [--encoding-type url] [-r] [--payer requester] [--version-id versionId] [-c file]
ossutil object-tagging --method get oss://bucket[/prefix] [--encoding-type url] [-r] [--payer requester] [--version-id versionId] [-c file]
ossuitl object-tagging --method delete oss://bucket[/prefix] [--encoding-type url] [-r] [--payer requester] [--version-id versionId] [-c file]
`,
detailHelpText: `
object-tagging command can set, get and delete the tag configuration of the oss object by set method option value to put, get, delete
the key and value of each tag must be separated by the character '#', you can enter up to 10 tag parameters.
Usage:
There are three usages for this command:
1) ossutil object-tagging --method put oss://bucket/object tagkey#tagvalue
The command sets the tag configuration of the object. The key and value are tagkey and tagvalue
2) ossutil object-tagging --method get oss://bucket/object
The command gets the tag configuration of object
3) ossutil object-tagging --method delete oss://bucket/object
The command deletes the tag configuration of bucket/object
`,
sampleText: `
1) set object tag configuration with one tag
ossutil object-tagging --method put oss://bucket/object tagkey#tagvalue
2) batch set objects tag configuration
ossutil object-tagging --method put oss://bucket/prefix -r tagkey#tagvalue
3) set object tag configuration with serveral tags
ossutil object-tagging --method put oss://bucket/object tagkey1#tagvalue1 tagkey2#tagvalue2
4) get object tag configuration
ossutil object-tagging --method get oss://bucket/object
5) batch get objects tag configuration
ossutil object-tagging --method get oss://bucket/prefix -r
6) delete object tag configuration
ossutil object-tagging --method delete oss://bucket/object
7) batch delete objects tag configuration
ossutil object-tagging --method delete oss://bucket/prefix -r
`,
}
type ObjectTagCommand struct {
command Command
monitor Monitor
method string
tagging oss.Tagging
commonOptions []oss.Option
lock sync.Mutex
printHeader bool
objectIndex int32
reportOption batchOptionType
}
var objectTagCommand = ObjectTagCommand{
command: Command{
name: "object-tagging",
nameAlias: []string{"object-tagging"},
minArgc: 1,
maxArgc: 11,
specChinese: specChineseObjectTag,
specEnglish: specEnglishObjectTag,
group: GroupTypeNormalCommand,
validOptionNames: []string{
OptionConfigFile,
OptionEndpoint,
OptionAccessKeyID,
OptionAccessKeySecret,
OptionSTSToken,
OptionProxyHost,
OptionProxyUser,
OptionProxyPwd,
OptionRoutines,
OptionMethod,
OptionLogLevel,
OptionEncodingType,
OptionRecursion,
OptionVersionId,
OptionRequestPayer,
OptionPassword,
OptionMode,
OptionECSRoleName,
OptionTokenTimeout,
OptionRamRoleArn,
OptionRoleSessionName,
OptionReadTimeout,
OptionConnectTimeout,
OptionSTSRegion,
OptionSkipVerfiyCert,
OptionUserAgent,
},
},
}
// function for FormatHelper interface
func (otc *ObjectTagCommand) formatHelpForWhole() string {
return otc.command.formatHelpForWhole()
}
func (otc *ObjectTagCommand) formatIndependHelp() string {
return otc.command.formatIndependHelp()
}
// Init simulate inheritance, and polymorphism
func (otc *ObjectTagCommand) Init(args []string, options OptionMapType) error {
return otc.command.Init(args, options, otc)
}
// RunCommand simulate inheritance, and polymorphism
func (otc *ObjectTagCommand) RunCommand() error {
otc.tagging.Tags = []oss.Tag{} // clear tags for test
otc.monitor.init("ObjectTagging")
encodingType, _ := GetString(OptionEncodingType, otc.command.options)
cloudUrL, err := GetCloudUrl(otc.command.args[0], encodingType)
if err != nil {
return err
}
strMethod, _ := GetString(OptionMethod, otc.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")
}
otc.method = strMethod
payer, _ := GetString(OptionRequestPayer, otc.command.options)
if payer != "" {
if payer != strings.ToLower(string(oss.Requester)) {
return fmt.Errorf("invalid request payer: %s, please check", payer)
}
otc.commonOptions = append(otc.commonOptions, oss.RequestPayer(oss.PayerType(payer)))
}
recursive, _ := GetBool(OptionRecursion, otc.command.options)
versionId, _ := GetString(OptionVersionId, otc.command.options)
if len(versionId) > 0 {
if recursive {
return fmt.Errorf("--version-id and -r can't be both used")
} else {
otc.commonOptions = append(otc.commonOptions, oss.VersionId(versionId))
}
}
if strMethod == "put" {
if len(otc.command.args) < 2 {
return fmt.Errorf("When the method value is put, there must be at least 2 parameters")
}
tagList := otc.command.args[1:len(otc.command.args)]
for _, tag := range tagList {
pSlice := strings.Split(tag, "#")
if len(pSlice) != 2 {
return fmt.Errorf("%s error,tag name and tag value must be separated by #", tag)
}
otc.tagging.Tags = append(otc.tagging.Tags, oss.Tag{Key: pSlice[0], Value: pSlice[1]})
}
}
bucket, err := otc.command.ossBucket(cloudUrL.bucket)
if err != nil {
return err
}
if !recursive {
err = otc.SingleObjectTagging(bucket, cloudUrL.object)
} else {
err = otc.BatchObjectTagging(bucket, *cloudUrL)
}
return err
}
func (otc *ObjectTagCommand) SingleObjectTagging(bucket *oss.Bucket, objectName string) error {
if len(objectName) == 0 {
return fmt.Errorf("object key is empty")
}
var tagError error
if otc.method == "put" {
tagError = bucket.PutObjectTagging(objectName, otc.tagging, otc.commonOptions...)
} else if otc.method == "get" {
resutl, err := bucket.GetObjectTagging(objectName, otc.commonOptions...)
tagError = err
if err == nil {
otc.lock.Lock()
if len(resutl.Tags) > 0 {
otc.objectIndex++
if !otc.printHeader {
fmt.Printf("%-15s%-15s%s\t%s\t%s\n", "object index", "tag index", "tag key", "tag value", "object")
fmt.Printf("---------------------------------------------------------------------------\n")
otc.printHeader = true
}
}
for index, tag := range resutl.Tags {
fmt.Printf("%-15d%-15d\"%s\"\t\"%s\"\t%s\n", otc.objectIndex, index, tag.Key, tag.Value, CloudURLToString(bucket.BucketName, objectName))
}
otc.lock.Unlock()
}
} else if otc.method == "delete" {
tagError = bucket.DeleteObjectTagging(objectName, otc.commonOptions...)
}
if tagError == nil {
LogInfo("%s tagging success,object:%s\n", otc.method, objectName)
} else {
LogError("%s tagging error,object:%s,error info:%s\n", otc.method, objectName, tagError.Error())
}
return tagError
}
func (otc *ObjectTagCommand) BatchObjectTagging(bucket *oss.Bucket, cloudURL CloudURL) error {
otc.reportOption.ctnu = true
outputDir, _ := GetString(OptionOutputDir, otc.command.options)
var err error
if otc.reportOption.reporter, err = GetReporter(otc.reportOption.ctnu, outputDir, commandLine); err != nil {
return err
}
defer otc.reportOption.reporter.Clear()
routines, _ := GetInt(OptionRoutines, otc.command.options)
chObjects := make(chan string, ChannelBuf)
chError := make(chan error, routines+1)
chListError := make(chan error, 1)
go otc.command.objectStatistic(bucket, cloudURL, &otc.monitor, []filterOptionType{}, otc.commonOptions...)
go otc.command.objectProducer(bucket, cloudURL, chObjects, chListError, []filterOptionType{}, otc.commonOptions...)
for i := 0; int64(i) < routines; i++ {
go otc.objectTaggingConsumer(bucket, chObjects, chError)
}
return otc.waitRoutinueComplete(chError, chListError, routines)
}
func (otc *ObjectTagCommand) objectTaggingConsumer(bucket *oss.Bucket, chObjects <-chan string, chError chan<- error) {
for object := range chObjects {
err := otc.objectTaggingWithReport(bucket, object)
if err != nil {
chError <- err
if !otc.reportOption.ctnu {
return
}
continue
}
}
chError <- nil
}
func (otc *ObjectTagCommand) objectTaggingWithReport(bucket *oss.Bucket, object string) error {
err := otc.SingleObjectTagging(bucket, object)
if otc.method != "get" {
otc.command.updateMonitor(err, &otc.monitor)
msg := fmt.Sprintf("%s %s object tagging", otc.method, CloudURLToString(bucket.BucketName, object))
if err == nil {
otc.command.report(msg, err, &otc.reportOption)
} else {
otc.command.report(msg, ObjectError{err, bucket.BucketName, object}, &otc.reportOption)
}
}
return ObjectError{err, bucket.BucketName, object}
}
func (otc *ObjectTagCommand) waitRoutinueComplete(chError, chListError <-chan error, routines int64) error {
completed := 0
var ferr error
for int64(completed) <= routines {
select {
case err := <-chListError:
if err != nil {
return err
}
completed++
case err := <-chError:
if err == nil {
completed++
} else {
ferr = err
if !otc.reportOption.ctnu {
fmt.Printf(otc.monitor.progressBar(true, errExit))
return err
}
}
}
}
return otc.formatResultPrompt(ferr)
}
func (otc *ObjectTagCommand) formatResultPrompt(err error) error {
if otc.method != "get" {
fmt.Printf(otc.monitor.progressBar(true, normalExit))
}
if err != nil && otc.reportOption.ctnu {
return nil
}
return err
}