diff --git a/cmd/auto-complete.go b/cmd/auto-complete.go index c15a7ccc..81be755f 100644 --- a/cmd/auto-complete.go +++ b/cmd/auto-complete.go @@ -508,6 +508,7 @@ var completeCmds = map[string]complete.Predictor{ "/quota/info": aliasCompleter, "/quota/clear": aliasCompleter, "/put": complete.PredictOr(s3Completer, fsCompleter), + "/get": complete.PredictOr(s3Completer, fsCompleter), } // flagsToCompleteFlags transforms a cli.Flag to complete.Flags diff --git a/cmd/common-methods.go b/cmd/common-methods.go index db2a0ef8..2f1cd25e 100644 --- a/cmd/common-methods.go +++ b/cmd/common-methods.go @@ -552,7 +552,7 @@ func uploadSourceToTargetURL(ctx context.Context, uploadOpts uploadSourceToTarge SSE: srcSSE, Zip: uploadOpts.isZip, }, - fetchStat: true, + fetchStat: !uploadOpts.ignoreStat, preserve: uploadOpts.preserve, }) if err != nil { @@ -606,7 +606,7 @@ func uploadSourceToTargetURL(ctx context.Context, uploadOpts uploadSourceToTarge multipartThreads: uint(multipartThreads), } - if isReadAt(reader) { + if isReadAt(reader) || length == 0 { _, err = putTargetStream(ctx, targetAlias, targetURL.String(), mode, until, legalHold, reader, length, uploadOpts.progress, putOpts) } else { @@ -685,4 +685,5 @@ type uploadSourceToTargetURLOpts struct { preserve, isZip bool multipartSize string multipartThreads string + ignoreStat bool } diff --git a/cmd/cp-main.go b/cmd/cp-main.go index 24a850e5..127e5285 100644 --- a/cmd/cp-main.go +++ b/cmd/cp-main.go @@ -266,7 +266,7 @@ func doCopy(ctx context.Context, copyOpts doCopyOpts) URLs { }) } - urls := uploadSourceToTargetURL(ctx, uploadSourceToTargetURLOpts{urls: copyOpts.cpURLs, progress: copyOpts.pg, encKeyDB: copyOpts.encKeyDB, preserve: copyOpts.preserve, isZip: copyOpts.isZip, multipartSize: copyOpts.multipartSize, multipartThreads: copyOpts.multipartThreads}) + urls := uploadSourceToTargetURL(ctx, uploadSourceToTargetURLOpts{urls: copyOpts.cpURLs, progress: copyOpts.pg, encKeyDB: copyOpts.encKeyDB, preserve: copyOpts.preserve, isZip: copyOpts.isZip, multipartSize: copyOpts.multipartSize, multipartThreads: copyOpts.multipartThreads, ignoreStat: copyOpts.ignoreStat}) if copyOpts.isMvCmd && urls.Error == nil { rmManager.add(ctx, sourceAlias, sourceURL.String()) } @@ -766,4 +766,5 @@ type doCopyOpts struct { isMvCmd, preserve, isZip bool multipartSize string multipartThreads string + ignoreStat bool } diff --git a/cmd/get-main.go b/cmd/get-main.go new file mode 100644 index 00000000..e21f0120 --- /dev/null +++ b/cmd/get-main.go @@ -0,0 +1,139 @@ +// Copyright (c) 2015-2024 MinIO, Inc. +// +// This file is part of MinIO Object Storage stack +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package cmd + +import ( + "context" + "strings" + + "github.com/minio/cli" + "github.com/minio/pkg/v2/console" +) + +// get command flags. +var ( + getFlags = []cli.Flag{} +) + +// Get command. +var getCmd = cli.Command{ + Name: "get", + Usage: "get s3 object to local", + Action: mainGet, + OnUsageError: onUsageError, + Before: setGlobalsFromContext, + Flags: append(append(ioFlags, globalFlags...), getFlags...), + CustomHelpTemplate: `NAME: + {{.HelpName}} - {{.Usage}} + +USAGE: + {{.HelpName}} [FLAGS] SOURCE TARGET + +FLAGS: + {{range .VisibleFlags}}{{.}} + {{end}} +ENVIRONMENT VARIABLES: + MC_ENCRYPT: list of comma delimited prefixes + MC_ENCRYPT_KEY: list of comma delimited prefix=secret values + +EXAMPLES: + 1. Get an object from S3 storage to local file system + {{.Prompt}} {{.HelpName}} ALIAS/BUCKET/object path-to/object +`, +} + +// mainGet is the entry point for get command. +func mainGet(cliCtx *cli.Context) error { + ctx, cancelGet := context.WithCancel(globalContext) + defer cancelGet() + + encKeyDB, err := getEncKeys(cliCtx) + fatalIf(err, "Unable to parse encryption keys.") + + args := cliCtx.Args() + if len(args) < 2 { + showCommandHelpAndExit(cliCtx, 1) // last argument is exit code. + } + // get source and target + sourceURLs := args[:len(args)-1] + targetURL := args[len(args)-1] + + getURLsCh := make(chan URLs, 10000) + var totalObjects, totalBytes int64 + + // Store a progress bar or an accounter + var pg ProgressReader + + // Enable progress bar reader only during default mode. + if !globalQuiet && !globalJSON { // set up progress bar + pg = newProgressBar(totalBytes) + } else { + pg = newAccounter(totalBytes) + } + go func() { + opts := prepareCopyURLsOpts{ + sourceURLs: sourceURLs, + targetURL: targetURL, + encKeyDB: encKeyDB, + ignoreBucketExistsCheck: true, + } + + for getURLs := range prepareGetURLs(ctx, opts) { + if getURLs.Error != nil { + printGetURLsError(&getURLs) + break + } + totalBytes += getURLs.SourceContent.Size + pg.SetTotal(totalBytes) + totalObjects++ + getURLsCh <- getURLs + } + close(getURLsCh) + }() + for { + select { + case <-ctx.Done(): + return nil + case getURLs, ok := <-getURLsCh: + if !ok { + return nil + } + urls := doCopy(ctx, doCopyOpts{cpURLs: getURLs, pg: pg, encKeyDB: encKeyDB, isMvCmd: false, preserve: false, isZip: false, ignoreStat: true}) + if urls.Error != nil { + return urls.Error.ToGoError() + } + } + } +} + +func printGetURLsError(cpURLs *URLs) { + // Print in new line and adjust to top so that we + // don't print over the ongoing scan bar + if !globalQuiet && !globalJSON { + console.Eraseline() + } + + if strings.Contains(cpURLs.Error.ToGoError().Error(), + " is a folder.") { + errorIf(cpURLs.Error.Trace(), + "Folder cannot be copied. Please use `...` suffix.") + } else { + errorIf(cpURLs.Error.Trace(), + "Unable to download.") + } +} diff --git a/cmd/get-url.go b/cmd/get-url.go new file mode 100644 index 00000000..d77194c3 --- /dev/null +++ b/cmd/get-url.go @@ -0,0 +1,125 @@ +// Copyright (c) 2015-2024 MinIO, Inc. +// +// This file is part of MinIO Object Storage stack +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package cmd + +import ( + "context" + "fmt" + + "github.com/minio/mc/pkg/probe" + "github.com/minio/minio-go/v7" +) + +// prepareGetURLs - prepares target and source clientURLs for copying. +func prepareGetURLs(ctx context.Context, o prepareCopyURLsOpts) chan URLs { + copyURLsCh := make(chan URLs) + go func(o prepareCopyURLsOpts) { + defer close(copyURLsCh) + copyURLsContent, err := guessGetURLType(ctx, o) + if err != nil { + copyURLsCh <- URLs{Error: err} + return + } + + switch copyURLsContent.copyType { + case copyURLsTypeA: + copyURLsCh <- prepareCopyURLsTypeA(ctx, *copyURLsContent, o) + case copyURLsTypeB: + copyURLsCh <- prepareCopyURLsTypeB(ctx, *copyURLsContent, o) + default: + copyURLsCh <- URLs{Error: errInvalidArgument().Trace(o.sourceURLs...)} + } + }(o) + + finalCopyURLsCh := make(chan URLs) + go func() { + defer close(finalCopyURLsCh) + for cpURLs := range copyURLsCh { + if cpURLs.Error != nil { + finalCopyURLsCh <- cpURLs + continue + } + finalCopyURLsCh <- cpURLs + } + }() + + return finalCopyURLsCh +} + +// guessGetURLType guesses the type of clientURL. This approach all allows prepareURL +// functions to accurately report failure causes. +func guessGetURLType(ctx context.Context, o prepareCopyURLsOpts) (*copyURLsContent, *probe.Error) { + cc := new(copyURLsContent) + + // Extract alias before fiddling with the clientURL. + cc.sourceURL = o.sourceURLs[0] + cc.sourceAlias, _, _ = mustExpandAlias(cc.sourceURL) + // Find alias and expanded clientURL. + cc.targetAlias, cc.targetURL, _ = mustExpandAlias(o.targetURL) + + if len(o.sourceURLs) == 1 { // 1 Source, 1 Target + var err *probe.Error + + client, err := newClient(cc.sourceURL) + if err != nil { + cc.copyType = copyURLsTypeInvalid + return cc, err + } + s3clnt, ok := client.(*S3Client) + if !ok { + return cc, probe.NewError(fmt.Errorf("Source is not s3.")) + } + bucket, path := s3clnt.url2BucketAndObject() + if bucket == "" { + return cc, probe.NewError(fmt.Errorf("Please set bucket for s3 resource.")) + } + if path == "" { + return cc, probe.NewError(fmt.Errorf("Please set a full path for s3 resource.")) + } + cc.sourceContent = s3clnt.objectInfo2ClientContent(bucket, minio.ObjectInfo{ + Key: path, + }) + + client, err = newClient(o.targetURL) + if err != nil { + cc.copyType = copyURLsTypeInvalid + return cc, err + } + _, ok = client.(*fsClient) + if !ok { + return cc, probe.NewError(fmt.Errorf("Target is not local filesystem.")) + } + + // If target is a folder, it is Type B. + var isDir bool + isDir, cc.targetContent = isAliasURLDir(ctx, o.targetURL, o.encKeyDB, o.timeRef, o.ignoreBucketExistsCheck) + if isDir { + cc.copyType = copyURLsTypeB + cc.sourceVersionID = cc.sourceContent.VersionID + return cc, nil + } + + // else Type A. + cc.copyType = copyURLsTypeA + cc.sourceVersionID = cc.sourceContent.VersionID + return cc, nil + } + + cc.copyType = copyURLsTypeInvalid + return cc, errInvalidArgument().Trace() +} diff --git a/cmd/main.go b/cmd/main.go index 9737910c..6c182c84 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -432,6 +432,7 @@ var appCmds = []cli.Command{ encryptCmd, eventCmd, findCmd, + getCmd, headCmd, ilmCmd, idpCmd,