From e10c82f9adbd6f0068ebcc26a547fdea46152381 Mon Sep 17 00:00:00 2001 From: Anis Elleuch Date: Tue, 16 Mar 2021 16:46:28 +0100 Subject: [PATCH] cp: Add --tags to tag uploaded objects (#3644) Applying tagging to objects copied in an S3 object using --tags flag. --- cmd/client-s3.go | 13 ++++++++ cmd/cp-main.go | 49 ++++++++++++++--------------- docs/minio-client-complete-guide.md | 1 + 3 files changed, 38 insertions(+), 25 deletions(-) diff --git a/cmd/client-s3.go b/cmd/client-s3.go index e36b4f53..06618638 100644 --- a/cmd/client-s3.go +++ b/cmd/client-s3.go @@ -944,6 +944,17 @@ func (c *S3Client) Put(ctx context.Context, reader io.Reader, size int64, metada delete(metadata, "X-Amz-Storage-Class") } + var tagsMap map[string]string + tagsHdr, ok := metadata["X-Amz-Tagging"] + if ok { + tagsSet, e := tags.Parse(tagsHdr, true) + if e != nil { + return 0, probe.NewError(e) + } + tagsMap = tagsSet.ToMap() + delete(metadata, "X-Amz-Tagging") + } + lockModeStr, ok := metadata[AmzObjectLockMode] lockMode := minio.RetentionMode("") if ok { @@ -959,8 +970,10 @@ func (c *S3Client) Put(ctx context.Context, reader io.Reader, size int64, metada retainUntilDate = t.UTC() } } + opts := minio.PutObjectOptions{ UserMetadata: metadata, + UserTags: tagsMap, Progress: progress, NumThreads: defaultMultipartThreadsNum, ContentType: contentType, diff --git a/cmd/cp-main.go b/cmd/cp-main.go index 915ce03c..6fbd7d95 100644 --- a/cmd/cp-main.go +++ b/cmd/cp-main.go @@ -86,6 +86,10 @@ var ( Name: "md5", Usage: "force all upload(s) to calculate md5sum checksum", }, + cli.StringFlag{ + Name: "tags", + Usage: "apply tags to the uploaded objects", + }, cli.StringFlag{ Name: rmFlag, Usage: "retention mode to be applied on the object (governance, compliance)", @@ -188,6 +192,9 @@ EXAMPLES: 19. Roll back 10 days in the past to copy the content of 'mybucket' {{.Prompt}} {{.HelpName}} --rewind 10d -r play/mybucket/ /tmp/dest/ + 20. Set tags to the uploaded objects + {{.Prompt}} {{.HelpName}} -r --tags "category=prod" ./data/ play/another-bucket/ + `, } @@ -411,7 +418,6 @@ func doCopySession(ctx context.Context, cancelCopy context.CancelFunc, cli *cli. cpURLsCh <- cpURLs } - }() } else { // Access recursive flag inside the session header. @@ -490,29 +496,20 @@ func doCopySession(ctx context.Context, cancelCopy context.CancelFunc, cli *cli. cpURLs.TargetContent.Metadata["X-Amz-Storage-Class"] = storageClass } - // update Object retention related fields - if session != nil { - cpURLs.TargetContent.RetentionMode = session.Header.CommandStringFlags[rmFlag] - if cpURLs.TargetContent.RetentionMode != "" { - cpURLs.TargetContent.RetentionEnabled = true - } - cpURLs.TargetContent.RetentionDuration = session.Header.CommandStringFlags[rdFlag] - cpURLs.TargetContent.LegalHold = strings.ToUpper(session.Header.CommandStringFlags[lhFlag]) - if cpURLs.TargetContent.LegalHold != "" { - cpURLs.TargetContent.LegalHoldEnabled = true - } - } else { - if rm := cli.String(rmFlag); rm != "" { - cpURLs.TargetContent.RetentionMode = rm - cpURLs.TargetContent.RetentionEnabled = true - } - if rd := cli.String(rdFlag); rd != "" { - cpURLs.TargetContent.RetentionDuration = rd - } - if lh := cli.String(lhFlag); lh != "" { - cpURLs.TargetContent.LegalHold = strings.ToUpper(lh) - cpURLs.TargetContent.LegalHoldEnabled = true - } + if rm := cli.String(rmFlag); rm != "" { + cpURLs.TargetContent.RetentionMode = rm + cpURLs.TargetContent.RetentionEnabled = true + } + if rd := cli.String(rdFlag); rd != "" { + cpURLs.TargetContent.RetentionDuration = rd + } + if lh := cli.String(lhFlag); lh != "" { + cpURLs.TargetContent.LegalHold = strings.ToUpper(lh) + cpURLs.TargetContent.LegalHoldEnabled = true + } + + if tags := cli.String("tags"); tags != "" { + cpURLs.TargetContent.Metadata["X-Amz-Tagging"] = tags } preserve := cli.Bool("preserve") @@ -762,6 +759,7 @@ func mainCopy(cliCtx *cli.Context) error { retentionMode := cliCtx.String(rmFlag) retentionDuration := cliCtx.String(rdFlag) legalHold := strings.ToUpper(cliCtx.String(lhFlag)) + tags := cliCtx.String("tags") sseKeys := os.Getenv("MC_ENCRYPT_KEY") if key := cliCtx.String("encrypt-key"); key != "" { sseKeys = key @@ -776,7 +774,7 @@ func mainCopy(cliCtx *cli.Context) error { var session *sessionV8 if cliCtx.Bool("continue") { - sessionID := getHash("cp", cliCtx.Args()) + sessionID := getHash("cp", os.Args[1:]) if isSessionExists(sessionID) { session, err = loadSessionV8(sessionID) fatalIf(err.Trace(sessionID), "Unable to load session.") @@ -789,6 +787,7 @@ func mainCopy(cliCtx *cli.Context) error { session.Header.CommandStringFlags["older-than"] = olderThan session.Header.CommandStringFlags["newer-than"] = newerThan session.Header.CommandStringFlags["storage-class"] = storageClass + session.Header.CommandStringFlags["tags"] = tags session.Header.CommandStringFlags[rmFlag] = retentionMode session.Header.CommandStringFlags[rdFlag] = retentionDuration session.Header.CommandStringFlags[lhFlag] = legalHold diff --git a/docs/minio-client-complete-guide.md b/docs/minio-client-complete-guide.md index a2319a05..e91252bf 100644 --- a/docs/minio-client-complete-guide.md +++ b/docs/minio-client-complete-guide.md @@ -811,6 +811,7 @@ FLAGS: --continue, -c create or resume copy session --encrypt value encrypt/decrypt objects (using server-side encryption with server managed keys) --encrypt-key value encrypt/decrypt objects (using server-side encryption with customer provided keys) + --tags value apply tags to the uploaded objects (eg. key=value&key2=value2, etc) --help, -h show help ENVIRONMENT VARIABLES: