Skip to content

Commit

Permalink
cp command support set tagging
Browse files Browse the repository at this point in the history
  • Loading branch information
taowei.wtw authored and kkuai committed Apr 9, 2020
1 parent 154a4e7 commit 23c20cd
Show file tree
Hide file tree
Showing 5 changed files with 303 additions and 6 deletions.
22 changes: 22 additions & 0 deletions lib/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,28 @@ func (cmd *Command) getOSSOptions(hopMap map[string]interface{}, headers map[str
return options, nil
}

func (cmd *Command) getOSSTagging(strTagging string) ([]oss.Tag, error) {
tags := []oss.Tag{}
strKeys := strings.Split(strTagging, "&")
for _, v := range strKeys {
if v == "" {
return tags, fmt.Errorf("tagging value is empty,maybe exist &&")
}
tagNode := strings.Split(v, "=")
if len(tagNode) >= 3 {
return tags, fmt.Errorf("tagging value error %s", v)
}

// value maybe empty
tagNode = append(tagNode, "")
tags = append(tags, oss.Tag{
Key: tagNode[0],
Value: tagNode[1],
})
}
return tags, nil
}

// GetAllCommands returns all commands list
func GetAllCommands() []interface{} {
return []interface{}{
Expand Down
1 change: 1 addition & 0 deletions lib/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const (
OptionRedundancyType = "redundancyType"
OptionDisableAllSymlink = "disableAllSymlink"
OptionDisableIgnoreError = "disableIgnoreError"
OptionTagging = "tagging"
)

// the elements show in stat object
Expand Down
39 changes: 39 additions & 0 deletions lib/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type copyOptionType struct {
disableDirObject bool
resumeProgress *OssResumeProgressListener
disableAllSymlink bool
tagging string
}

type filterOptionType struct {
Expand Down Expand Up @@ -250,6 +251,11 @@ var specChineseCopy = SpecText{
注意:header不区分大小写,但value区分大小写。设置后将用指定的meta代替原来的meta。没有指定的
HTTP HEADER将保留,没有指定的user meta将会被删除。
--tagging选项
该选项在上传文件的同时设置object的tagging信息。当指定--recursive选项时,会设置所有上传的
objects的tagging信息。
如果一次设置多个tagging,必须使用双引号,比如 "tagA=A&tagB=B"
--acl选项
该选项在上传文件的同时设置object的acl信息。当指定--recursive选项时,会设置所有上传的
Expand Down Expand Up @@ -527,6 +533,9 @@ var specChineseCopy = SpecText{
ossutil cp local_dir oss://bucket1/b -r --disable-all-symlink
忽略所有的链接子文件以及链接子目录
ossutil cp local_dir oss://bucket1/b --tagging "tagA=A&tagB=B" -r
上传的同时设置两个tagging,key分别为tagA和tagB,value分别为A和B
2) 从oss下载object
假设oss上有下列objects:
oss://bucket/abcdir1/a
Expand Down Expand Up @@ -653,6 +662,9 @@ var specChineseCopy = SpecText{
ossutil cp oss://bucket/dir/ oss://bucket1/ -r --only-current-dir
只copy当前目录下的object, 忽略其他子目录
ossutil cp oss://bucket/object1 oss://bucket/object2 --tagging "tagA=A&tagB=B"
copy的同时设置两个tagging,key分别为tagA和tagB,value分别为A和B
`,
}

Expand Down Expand Up @@ -777,6 +789,12 @@ var specEnglishCopy = SpecText{
replaced with specified meta. HTTP HEADER will be reserved if no speified value. User meta will be
deleted if no specified value.
--tagging option
This option will set the specified objects' tagging data. If --recursive option is specified,
ossutil will set tagging for all uploaded objects.
If you set more than one tagging at a time, you must use double quotes, such as "tagA=A&tagB=B"
--acl option
This option will set acl on the specified objects. If --recursive option is specified,
Expand Down Expand Up @@ -1083,6 +1101,9 @@ Usage:
ossutil cp local_dir oss://bucket1/b -r --disable-all-symlink
uploading of symlink files and symlink directories under the local_dir is not allowed
ossutil cp local_dir oss://bucket/b --tagging "tagA=A&tagB=B"
Set two taggings when uploading, the key is tagA and tagB, and the value is A and B
2) download from oss
Suppose there are following objects in oss:
oss://bucket/abcdir1/a
Expand Down Expand Up @@ -1205,6 +1226,9 @@ Usage:
ossutil cp oss://bucket/dir/ oss://bucket1/ -r --only-current-dir
Copy only the object in the current directory, ignoring other subdirectories
ossutil cp oss://bucket/object1 oss://bucket/object2 --tagging "tagA=A&tagB=B"
Set two taggings when copying, the key is tagA and tagB, and the value is A and B
`,
}

Expand Down Expand Up @@ -1263,6 +1287,7 @@ var copyCommand = CopyCommand{
OptionDisableDirObject,
OptionDisableAllSymlink,
OptionDisableIgnoreError,
OptionTagging,
},
},
}
Expand Down Expand Up @@ -1299,6 +1324,7 @@ func (cc *CopyCommand) RunCommand() error {
cc.cpOption.vrange, _ = GetString(OptionRange, cc.command.options)
cc.cpOption.encodingType, _ = GetString(OptionEncodingType, cc.command.options)
cc.cpOption.meta, _ = GetString(OptionMeta, cc.command.options)
cc.cpOption.tagging, _ = GetString(OptionTagging, cc.command.options)
acl, _ := GetString(OptionACL, cc.command.options)
payer, _ := GetString(OptionRequestPayer, cc.command.options)
cc.cpOption.partitionInfo, _ = GetString(OptionPartitionDownload, cc.command.options)
Expand Down Expand Up @@ -1363,6 +1389,18 @@ func (cc *CopyCommand) RunCommand() error {
cc.cpOption.options = append(cc.cpOption.options, topts...)
}

if cc.cpOption.tagging != "" {
if opType == operationTypeGet {
return fmt.Errorf("No need to set tagging for download")
}
tags, err := cc.command.getOSSTagging(cc.cpOption.tagging)
if err != nil {
return err
}
tagging := oss.Tagging{Tags: tags}
cc.cpOption.options = append(cc.cpOption.options, oss.SetTagging(tagging))
}

if acl != "" {
if opType == operationTypeGet {
return fmt.Errorf("No need to set ACL for download")
Expand Down Expand Up @@ -2931,6 +2969,7 @@ func (cc *CopyCommand) ossCopyObjectRetry(bucket *oss.Bucket, objectName, destBu
retryTimes, _ := GetInt(OptionRetryTimes, cc.command.options)
options := cc.cpOption.options
options = append(options, oss.MetadataDirective(oss.MetaReplace))
options = append(options, oss.TaggingDirective(oss.TaggingReplace))
for i := 1; ; i++ {
if i > 1 {
time.Sleep(time.Duration(1) * time.Second)
Expand Down
Loading

0 comments on commit 23c20cd

Please sign in to comment.