diff --git a/cmd/client-s3.go b/cmd/client-s3.go index 9bcba4c0..76d15d1f 100644 --- a/cmd/client-s3.go +++ b/cmd/client-s3.go @@ -608,6 +608,10 @@ func (c *s3Client) Put(ctx context.Context, reader io.Reader, size int64, metada delete(metadata, "Content-Language") } + storageClass, ok := metadata["X-Amz-Storage-Class"] + if ok { + delete(metadata, "X-Amz-Storage-Class") + } if bucket == "" { return 0, probe.NewError(BucketNameEmpty{}) } @@ -620,6 +624,7 @@ func (c *s3Client) Put(ctx context.Context, reader io.Reader, size int64, metada ContentDisposition: contentDisposition, ContentEncoding: contentEncoding, ContentLanguage: contentLanguage, + StorageClass: strings.ToUpper(storageClass), } n, e := c.api.PutObjectWithContext(ctx, bucket, object, reader, size, opts) if e != nil { diff --git a/cmd/common-methods.go b/cmd/common-methods.go index 2d6d73b5..db1f24a4 100644 --- a/cmd/common-methods.go +++ b/cmd/common-methods.go @@ -164,6 +164,12 @@ func uploadSourceToTargetURL(ctx context.Context, urls URLs, progress io.Reader) if err != nil { return urls.WithError(err.Trace(sourceURL.String())) } + // Get metadata from target content as well + if urls.TargetContent.Metadata != nil { + for k, v := range urls.TargetContent.Metadata { + metadata[k] = v + } + } _, err = putTargetStream(ctx, targetAlias, targetURL.String(), reader, length, metadata, progress) if err != nil { return urls.WithError(err.Trace(targetURL.String())) diff --git a/cmd/cp-main.go b/cmd/cp-main.go index e20d7b4e..f22b79d4 100644 --- a/cmd/cp-main.go +++ b/cmd/cp-main.go @@ -49,6 +49,10 @@ var ( Name: "newer-than", Usage: "Copy objects newer than N days", }, + cli.StringFlag{ + Name: "storage-class, sc", + Usage: "Set storage class for object", + }, } ) @@ -317,6 +321,14 @@ func doCopySession(session *sessionV8) error { // Save totalSize. cpURLs.TotalSize = session.Header.TotalBytes + // Check and handle storage class if passed in command line args + if _, ok := session.Header.CommandStringFlags["storage-class"]; ok { + if cpURLs.TargetContent.Metadata == nil { + cpURLs.TargetContent.Metadata = make(map[string]string) + } + cpURLs.TargetContent.Metadata["X-Amz-Storage-Class"] = session.Header.CommandStringFlags["storage-class"] + } + // Verify if previously copied, notify progress bar. if isCopied(cpURLs.SourceContent.URL.String()) { queueCh <- func() URLs { @@ -407,12 +419,14 @@ func mainCopy(ctx *cli.Context) error { recursive := ctx.Bool("recursive") olderThan := ctx.Int("older-than") newerThan := ctx.Int("newer-than") + storageClass := ctx.String("storage-class") session := newSessionV8() session.Header.CommandType = "cp" session.Header.CommandBoolFlags["recursive"] = recursive session.Header.CommandIntFlags["older-than"] = olderThan session.Header.CommandIntFlags["newer-than"] = newerThan + session.Header.CommandStringFlags["storage-class"] = storageClass var e error if session.Header.RootPath, e = os.Getwd(); e != nil { diff --git a/cmd/mirror-main.go b/cmd/mirror-main.go index d3fb581a..46f2072f 100644 --- a/cmd/mirror-main.go +++ b/cmd/mirror-main.go @@ -78,6 +78,10 @@ var ( Name: "newer-than", Usage: "Select objects newer than N days", }, + cli.StringFlag{ + Name: "storage-class, sc", + Usage: "Set storage class for object", + }, } ) @@ -170,6 +174,7 @@ type mirrorJob struct { isFake, isRemove, isOverwrite, isWatch bool olderThan, newerThan int + storageClass string excludeOptions []string } @@ -237,6 +242,13 @@ func (mj *mirrorJob) doMirror(ctx context.Context, cancelMirror context.CancelFu mj.status.SetCaption(sourceURL.String() + ": ") + if mj.storageClass != "" { + if sURLs.TargetContent.Metadata == nil { + sURLs.TargetContent.Metadata = make(map[string]string) + } + sURLs.TargetContent.Metadata["X-Amz-Storage-Class"] = mj.storageClass + } + sourcePath := filepath.ToSlash(filepath.Join(sourceAlias, sourceURL.Path)) targetPath := filepath.ToSlash(filepath.Join(targetAlias, targetURL.Path)) mj.status.PrintMsg(mirrorMessage{ @@ -553,7 +565,7 @@ func (mj *mirrorJob) mirror(ctx context.Context, cancelMirror context.CancelFunc } } -func newMirrorJob(srcURL, dstURL string, isFake, isRemove, isOverwrite, isWatch bool, excludeOptions []string, olderThan, newerThan int) *mirrorJob { +func newMirrorJob(srcURL, dstURL string, isFake, isRemove, isOverwrite, isWatch bool, excludeOptions []string, olderThan, newerThan int, storageClass string) *mirrorJob { // we'll define the status to use here, // do we want the quiet status? or the progressbar var status = NewProgressStatus() @@ -577,12 +589,12 @@ func newMirrorJob(srcURL, dstURL string, isFake, isRemove, isOverwrite, isWatch excludeOptions: excludeOptions, olderThan: olderThan, newerThan: newerThan, - - status: status, - statusCh: make(chan URLs), - queueCh: make(chan func() URLs), - wgStatus: new(sync.WaitGroup), - watcher: NewWatcher(UTCNow()), + storageClass: storageClass, + status: status, + statusCh: make(chan URLs), + queueCh: make(chan func() URLs), + wgStatus: new(sync.WaitGroup), + watcher: NewWatcher(UTCNow()), } return &mj @@ -629,7 +641,8 @@ func runMirror(srcURL, dstURL string, ctx *cli.Context) *probe.Error { ctx.Bool("watch"), ctx.StringSlice("exclude"), ctx.Int("older-than"), - ctx.Int("newer-than")) + ctx.Int("newer-than"), + ctx.String("storage-class")) srcClt, err := newClient(srcURL) fatalIf(err, "Unable to initialize `"+srcURL+"`") diff --git a/docs/minio-client-complete-guide.md b/docs/minio-client-complete-guide.md index e901d92c..63f33063 100644 --- a/docs/minio-client-complete-guide.md +++ b/docs/minio-client-complete-guide.md @@ -352,16 +352,25 @@ USAGE: mc cp [FLAGS] SOURCE [SOURCE...] TARGET FLAGS: - --help, -h Show help. - --recursive, -r Copy recursively. + --recursive, -r Copy recursively. + --storage-class value, -sc value Set storage class for object. + --help, -h Show help. ``` -*Example: Copy a text file to to an object storage.* +*Example: Copy a text file to an object storage.* ```sh mc cp myobject.txt play/mybucket myobject.txt: 14 B / 14 B ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ 100.00 % 41 B/s 0 ``` + +*Example: Copy a text file to an object storage and assign storage-class `REDUCED_REDUNDANCY` to the uploaded object.* + +```sh +mc cp --storage-class REDUCED_REDUNDANCY myobject.txt play/mybucket +myobject.txt: 14 B / 14 B ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ 100.00 % 41 B/s 0 +``` + ### Command `rm` - Remove Buckets and Objects Use `rm` command to remove file or bucket @@ -495,12 +504,13 @@ USAGE: mc mirror [FLAGS] SOURCE TARGET FLAGS: - --help, -h Show help. - --force Force overwrite of an existing target(s). - --fake Perform a fake mirror operation. - --watch, -w Watch and mirror for changes. - --remove Remove extraneous file(s) on target. -``` + --help, -h Show help. + --force Force overwrite of an existing target(s). + --fake Perform a fake mirror operation. + --watch, -w Watch and mirror for changes. + --remove Remove extraneous file(s) on target. + --storage-class value, --sc value Set storage class for object. +``` *Example: Mirror a local directory to 'mybucket' on https://play.minio.io:9000.* diff --git a/functional-tests.sh b/functional-tests.sh index 7f790e79..558aca99 100755 --- a/functional-tests.sh +++ b/functional-tests.sh @@ -289,6 +289,31 @@ function test_put_object_multipart() log_success "$start_time" "${FUNCNAME[0]}" } +## Test mc cp command with storage-class flag set +function test_put_object_with_storage_class() +{ + show "${FUNCNAME[0]}" + + start_time=$(get_time) + object_name="mc-test-object-$RANDOM" + assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd cp --storage-class REDUCED_REDUNDANCY "${FILE_1_MB}" "${SERVER_ALIAS}/${BUCKET_NAME}/${object_name}" + assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd rm "${SERVER_ALIAS}/${BUCKET_NAME}/${object_name}" + + log_success "$start_time" "${FUNCNAME[0]}" +} + +## Test mc cp command with storage-class flag set to incorrect value +function test_put_object_with_storage_class_error() +{ + show "${FUNCNAME[0]}" + + start_time=$(get_time) + object_name="mc-test-object-$RANDOM" + assert_failure "$start_time" "${FUNCNAME[0]}" mc_cmd cp --storage-class REDUCED "${FILE_1_MB}" "${SERVER_ALIAS}/${BUCKET_NAME}/${object_name}" + + log_success "$start_time" "${FUNCNAME[0]}" +} + function test_get_object() { show "${FUNCNAME[0]}" @@ -423,6 +448,25 @@ function test_mirror_list_objects() log_success "$start_time" "${FUNCNAME[0]}" } +## Tests mc mirror command with --storage-class flag set +function test_mirror_list_objects_storage_class() +{ + show "${FUNCNAME[0]}" + + start_time=$(get_time) + bucket_name="mc-test-bucket-$RANDOM" + object_name="mc-test-object-$RANDOM" + assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd mb "${SERVER_ALIAS}/${bucket_name}" + assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd mirror --storage-class REDUCED_REDUNDANCY "$DATA_DIR" "${SERVER_ALIAS}/${bucket_name}" + + diff -bB <(ls "$DATA_DIR") <("${MC_CMD[@]}" --json ls "${SERVER_ALIAS}/${bucket_name}" | jq -r .key) >/dev/null 2>&1 + assert_success "$start_time" "${FUNCNAME[0]}" show_on_failure $? "mirror and list differs" + + assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd rm --force --recursive "${SERVER_ALIAS}/${bucket_name}" + + log_success "$start_time" "${FUNCNAME[0]}" +} + ## Tests find command with --older set to 1day, should be empty. function test_find_empty() { show "${FUNCNAME[0]}" @@ -539,6 +583,8 @@ function run_test() test_put_object test_put_object_error + test_put_object_with_storage_class + test_put_object_with_storage_class_error test_put_object_multipart test_get_object test_get_object_multipart @@ -547,6 +593,7 @@ function run_test() test_presigned_get_object test_cat_object test_mirror_list_objects + test_mirror_list_objects_storage_class test_find test_find_empty if [ -z "$MINT_MODE" ]; then