share/download: add --starts-with flag and remove '...' we don't have that anymore.

- Fixes bug when prefix is available and should be prepended to the key.
- Fixes another bug to avoid doing URL stat for upload, its not needed.

Fixes #1478
This commit is contained in:
Harshavardhana
2015-11-23 12:33:08 -08:00
parent 598be201f3
commit aa3007bcd0
6 changed files with 45 additions and 32 deletions
+1 -1
View File
@@ -65,7 +65,7 @@ NOTE: Source installation is intended for only developers and advanced users.
If you do not have a working Golang environment, please follow [Install Golang](./INSTALLGO.md).
```sh
$ go get -u github.com/minio/mc
$ GO15VENDOREXPERIMENT=1 go get -u github.com/minio/mc
```
## Public Minio Server
+1 -1
View File
@@ -160,7 +160,7 @@ func (f *fsClient) ShareDownload(expires time.Duration) (string, *probe.Error) {
}
// ShareUpload - share upload not implemented for filesystem.
func (f *fsClient) ShareUpload(recursive bool, expires time.Duration, contentType string) (map[string]string, *probe.Error) {
func (f *fsClient) ShareUpload(startsWith bool, expires time.Duration, contentType string) (map[string]string, *probe.Error) {
return nil, probe.NewError(client.APINotImplemented{API: "ShareUpload", APIType: "filesystem"})
}
+10 -8
View File
@@ -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(recursive bool, expires time.Duration, contentType string) (map[string]string, *probe.Error) {
func (c *s3Client) ShareUpload(startsWith 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,13 +140,15 @@ func (c *s3Client) ShareUpload(recursive bool, expires time.Duration, contentTyp
if err := p.SetBucket(bucket); err != nil {
return nil, probe.NewError(err)
}
if recursive {
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 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)
}
}
}
m, err := c.api.PresignedPostPolicy(p)
+2 -2
View File
@@ -28,7 +28,7 @@ var (
Name: "help, h",
Usage: "Help of share download",
}
shareFlagRecursive = cli.BoolFlag{
shareFlagDownloadRecursive = cli.BoolFlag{
Name: "recursive, r",
Usage: "Share all objects recursively.",
}
@@ -39,7 +39,7 @@ var shareDownload = cli.Command{
Name: "download",
Usage: "Generate URLs for download access.",
Action: mainShareDownload,
Flags: append(globalFlags, shareFlagRecursive, shareFlagExpire, shareFlagDownloadHelp),
Flags: append(globalFlags, shareFlagExpire, shareFlagDownloadHelp, shareFlagDownloadRecursive),
CustomHelpTemplate: `NAME:
mc share {{.Name}} - {{.Usage}}
+30 -19
View File
@@ -18,6 +18,7 @@ package main
import (
"fmt"
"strings"
"time"
"github.com/minio/cli"
@@ -30,6 +31,10 @@ var (
Name: "help, h",
Usage: "Help of share download.",
}
shareFlagUploadStartsWith = cli.BoolFlag{
Name: "starts-with",
Usage: "Enable to upload to a particular key prefix.",
}
)
// Share documents via URL.
@@ -37,7 +42,7 @@ var shareUpload = cli.Command{
Name: "upload",
Usage: "Generate ‘curl’ command to upload objects without requiring access/secret keys.",
Action: mainShareUpload,
Flags: append(globalFlags, shareFlagExpire, shareFlagContentType, shareFlagUploadHelp),
Flags: append(globalFlags, shareFlagExpire, shareFlagContentType, shareFlagUploadHelp, shareFlagUploadStartsWith),
CustomHelpTemplate: `NAME:
mc share {{.Name}} - {{.Usage}}
@@ -48,14 +53,17 @@ OPTIONS:
{{range .Flags}}{{.}}
{{end}}
EXAMPLES:
1. Generate a curl command to allow upload access for a single object. Command expires after 7 days (default).
1. Generate a curl command to allow upload access for a single object. Command expires in 7 days (default).
$ mc share {{.Name}} s3.amazonaws.com/backup/2006-Mar-1/backup.tar.gz
2. Generate a curl command to allow upload access to a folder. Command expires in 120 hours.
$ mc share {{.Name}} --expire=120h s3.amazonaws.com/backup/2007-Mar-2/...
$ mc share {{.Name}} --expire=120h s3.amazonaws.com/backup/2007-Mar-2/
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/...
$ 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 of only objects with key prefix 'backup'. Command expires in 2 hours.
$ mc share {{.Name}} --starts-with --expire=2h s3.amazonaws.com/backup/2007-Mar-2/backup
`,
}
@@ -67,7 +75,6 @@ func checkShareUploadSyntax(ctx *cli.Context) {
}
// Parse expiry.
// isRecursive := ctx.Bool("recursive")
expiry := shareDefaultExpiry
expireArg := ctx.String("expire")
if expireArg != "" {
@@ -84,17 +91,12 @@ func checkShareUploadSyntax(ctx *cli.Context) {
fatalIf(errDummy().Trace(expiry.String()), "Expiry cannot be larger than 7 days.")
}
URLs, err := args2URLs(ctx.Args()) // expand alias.
_, err := args2URLs(ctx.Args()) // expand alias.
fatalIf(err.Trace(ctx.Args()...), "Unable to convert args to URLs.")
for _, url := range URLs {
_, _, err := url2Stat(url)
fatalIf(err.Trace(url), "Unable to stat ‘"+url+"’.")
}
}
// makeCurlCmd constructs curl command-line.
func makeCurlCmd(key string, uploadInfo map[string]string) string {
func makeCurlCmd(key string, startsWith bool, uploadInfo map[string]string) string {
URL := client.NewURL(key)
postURL := URL.Scheme + URL.SchemeSeparator + URL.Host + string(URL.Separator)
if !isBucketVirtualStyle(URL.Host) {
@@ -109,8 +111,17 @@ func makeCurlCmd(key string, uploadInfo map[string]string) string {
}
curlCommand += fmt.Sprintf("-F %s=%s ", k, v)
}
curlCommand += fmt.Sprintf("-F key=%s ", key) // Object name.
curlCommand += "-F file=@<FILE>" // File to upload.
// If key starts with is enabled prefix it with the output.
if startsWith {
curlCommand += fmt.Sprintf("-F key=%s<KEY> ", key) // Object name.
} else {
if strings.HasSuffix(key, string(URL.Separator)) {
curlCommand += fmt.Sprintf("-F key=%s<KEY> ", key) // Object name.
} else {
curlCommand += fmt.Sprintf("-F key=%s ", key) // Object name.
}
}
curlCommand += "-F file=@<FILE>" // File to upload.
return curlCommand
}
@@ -130,20 +141,20 @@ func saveSharedURL(objectURL string, shareURL string, expiry time.Duration, cont
}
// doShareUploadURL uploads files to the target.
func doShareUploadURL(objectURL string, recursive bool, expiry time.Duration, contentType string) *probe.Error {
func doShareUploadURL(objectURL string, startsWith 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(recursive, expiry, contentType)
uploadInfo, err := clnt.ShareUpload(startsWith, expiry, contentType)
if err != nil {
return err.Trace(objectURL, "expiry="+expiry.String(), "contentType="+contentType)
}
// Generate curl command.
curlCmd := makeCurlCmd(objectURL, uploadInfo)
curlCmd := makeCurlCmd(objectURL, startsWith, uploadInfo)
printMsg(shareMesssage{
ObjectURL: objectURL,
@@ -171,7 +182,7 @@ func mainShareUpload(ctx *cli.Context) {
shareSetColor()
// Set command flags from context.
isRecursive := ctx.Bool("recursive")
startsWith := ctx.Bool("starts-with")
expireArg := ctx.String("expire")
expiry := shareDefaultExpiry
contentType := ctx.String("content-type")
@@ -185,7 +196,7 @@ func mainShareUpload(ctx *cli.Context) {
fatalIf(err.Trace(ctx.Args()...), "Unable to convert args to URLs.")
for _, targetURL := range URLs {
err := doShareUploadURL(targetURL, isRecursive, expiry, contentType)
err := doShareUploadURL(targetURL, startsWith, expiry, contentType)
fatalIf(err.Trace(targetURL), "Unable to generate curl command for upload ‘"+targetURL+"’.")
}
}
+1 -1
View File
@@ -66,7 +66,7 @@ func (s shareMesssage) String() string {
msg += console.Colorize("Content-type", fmt.Sprintf("Content-Type: %s\n", s.ContentType))
}
// Highlight <FILE> specially. "share upload" sub-commands uses this identifier.
// Highlight <FILE> specifically. "share upload" sub-commands use this identifier.
shareURL := strings.Replace(s.ShareURL, "<FILE>", console.Colorize("File", "<FILE>"), 1)
msg += console.Colorize("Share", fmt.Sprintf("Share: %s\n", shareURL))