diff --git a/pkg/client/s3/s3.go b/pkg/client/s3/s3.go index 72f23c29..8cba3392 100644 --- a/pkg/client/s3/s3.go +++ b/pkg/client/s3/s3.go @@ -127,7 +127,7 @@ func (c *s3Client) ShareDownload(expires time.Duration) (string, *probe.Error) { } // ShareUpload - get data for presigned post http form upload. -func (c *s3Client) ShareUpload(startsWith bool, expires time.Duration, contentType string) (map[string]string, *probe.Error) { +func (c *s3Client) ShareUpload(isRecursive bool, expires time.Duration, contentType string) (map[string]string, *probe.Error) { bucket, object := c.url2BucketAndObject() p := minio.NewPostPolicy() if err := p.SetExpires(time.Now().UTC().Add(expires)); err != nil { @@ -140,15 +140,13 @@ func (c *s3Client) ShareUpload(startsWith bool, expires time.Duration, contentTy if err := p.SetBucket(bucket); err != nil { return nil, probe.NewError(err) } - if object != "" { - if startsWith { - if err := p.SetKeyStartsWith(object); err != nil { - return nil, probe.NewError(err) - } - } else { - if err := p.SetKey(object); err != nil { - return nil, probe.NewError(err) - } + if isRecursive { + if err := p.SetKeyStartsWith(object); err != nil { + return nil, probe.NewError(err) + } + } else { + if err := p.SetKey(object); err != nil { + return nil, probe.NewError(err) } } m, err := c.api.PresignedPostPolicy(p) diff --git a/share-upload-main.go b/share-upload-main.go index 36b06801..1639f28e 100644 --- a/share-upload-main.go +++ b/share-upload-main.go @@ -34,7 +34,7 @@ var ( }, cli.BoolFlag{ Name: "recursive, r", - Usage: "Recursively upload any object matching the prefix recursively.", + Usage: "Recursively upload any object matching the prefix.", }, shareFlagExpire, shareFlagContentType, @@ -66,8 +66,8 @@ EXAMPLES: 3. Generate a curl command to allow upload access of only '.png' images to a folder. Command expires in 2 hours. $ mc share {{.Name}} --expire=2h --content-type=image/png s3.amazonaws.com/backup/2007-Mar-2/ - 4. Generate a curl command to allow upload access to any objects matching the key prefix 'backup'. Command expires in 2 hours. - $ mc share {{.Name}} --recursive --expire=2h s3.amazonaws.com/backup/2007-Mar-2/backup + 4. Generate a curl command to allow upload access to any objects matching the key prefix 'backup/'. Command expires in 2 hours. + $ mc share {{.Name}} --recursive --expire=2h s3.amazonaws.com/backup/2007-Mar-2/backup/ `, } @@ -78,9 +78,12 @@ func checkShareUploadSyntax(ctx *cli.Context) { cli.ShowCommandHelpAndExit(ctx, "upload", 1) // last argument is exit code. } + // Set command flags from context. + isRecursive := ctx.Bool("recursive") + expireArg := ctx.String("expire") + // Parse expiry. expiry := shareDefaultExpiry - expireArg := ctx.String("expire") if expireArg != "" { var e error expiry, e = time.ParseDuration(expireArg) @@ -89,18 +92,28 @@ func checkShareUploadSyntax(ctx *cli.Context) { // Validate expiry. if expiry.Seconds() < 1 { - fatalIf(errDummy().Trace(expiry.String()), "Expiry cannot be lesser than 1 second.") + fatalIf(errDummy().Trace(expiry.String()), + "Expiry cannot be lesser than 1 second.") } if expiry.Seconds() > 604800 { - fatalIf(errDummy().Trace(expiry.String()), "Expiry cannot be larger than 7 days.") + fatalIf(errDummy().Trace(expiry.String()), + "Expiry cannot be larger than 7 days.") } - _, err := args2URLs(ctx.Args()) // expand alias. + URLs, err := args2URLs(ctx.Args()) // expand alias. fatalIf(err.Trace(ctx.Args()...), "Unable to convert args to URLs.") + + for _, targetURL := range URLs { + url := client.NewURL(targetURL) + if strings.HasSuffix(targetURL, string(url.Separator)) && !isRecursive { + fatalIf(errInvalidArgument().Trace(targetURL), + "Use --recursive option to generate curl command for prefixes.") + } + } } // makeCurlCmd constructs curl command-line. -func makeCurlCmd(key string, startsWith bool, uploadInfo map[string]string) string { +func makeCurlCmd(key string, isRecursive bool, uploadInfo map[string]string) string { URL := client.NewURL(key) postURL := URL.Scheme + URL.SchemeSeparator + URL.Host + string(URL.Separator) if !isBucketVirtualStyle(URL.Host) { @@ -116,14 +129,10 @@ func makeCurlCmd(key string, startsWith bool, uploadInfo map[string]string) stri curlCommand += fmt.Sprintf("-F %s=%s ", k, v) } // If key starts with is enabled prefix it with the output. - if startsWith { - curlCommand += fmt.Sprintf("-F key=%s ", key) // Object name. + if isRecursive { + curlCommand += fmt.Sprintf("-F key=%s ", key) // Object name. } else { - if strings.HasSuffix(key, string(URL.Separator)) { - curlCommand += fmt.Sprintf("-F key=%s ", key) // Object name. - } else { - curlCommand += fmt.Sprintf("-F key=%s ", key) // Object name. - } + curlCommand += fmt.Sprintf("-F key=%s ", key) // Object name. } curlCommand += "-F file=@" // File to upload. return curlCommand @@ -145,20 +154,20 @@ func saveSharedURL(objectURL string, shareURL string, expiry time.Duration, cont } // doShareUploadURL uploads files to the target. -func doShareUploadURL(objectURL string, startsWith bool, expiry time.Duration, contentType string) *probe.Error { +func doShareUploadURL(objectURL string, isRecursive bool, expiry time.Duration, contentType string) *probe.Error { clnt, err := url2Client(objectURL) if err != nil { return err.Trace(objectURL) } // Generate pre-signed access info. - uploadInfo, err := clnt.ShareUpload(startsWith, expiry, contentType) + uploadInfo, err := clnt.ShareUpload(isRecursive, expiry, contentType) if err != nil { return err.Trace(objectURL, "expiry="+expiry.String(), "contentType="+contentType) } // Generate curl command. - curlCmd := makeCurlCmd(objectURL, startsWith, uploadInfo) + curlCmd := makeCurlCmd(objectURL, isRecursive, uploadInfo) printMsg(shareMesssage{ ObjectURL: objectURL, @@ -186,7 +195,7 @@ func mainShareUpload(ctx *cli.Context) { shareSetColor() // Set command flags from context. - startsWith := ctx.Bool("starts-with") + isRecursive := ctx.Bool("recursive") expireArg := ctx.String("expire") expiry := shareDefaultExpiry contentType := ctx.String("content-type") @@ -200,7 +209,7 @@ func mainShareUpload(ctx *cli.Context) { fatalIf(err.Trace(ctx.Args()...), "Unable to convert args to URLs.") for _, targetURL := range URLs { - err := doShareUploadURL(targetURL, startsWith, expiry, contentType) + err := doShareUploadURL(targetURL, isRecursive, expiry, contentType) fatalIf(err.Trace(targetURL), "Unable to generate curl command for upload ‘"+targetURL+"’.") } } diff --git a/share.go b/share.go index f4d05ca0..11e67e65 100644 --- a/share.go +++ b/share.go @@ -68,6 +68,9 @@ func (s shareMesssage) String() string { // Highlight specifically. "share upload" sub-commands use this identifier. shareURL := strings.Replace(s.ShareURL, "", console.Colorize("File", ""), 1) + // Highlight specifically for recursive operation. + shareURL = strings.Replace(shareURL, "", console.Colorize("File", ""), 1) + msg += console.Colorize("Share", fmt.Sprintf("Share: %s\n", shareURL)) return msg