Skip to content

add min-len and max-len parameters to mc share upload #5184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/client-fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ func (f *fsClient) ShareDownload(_ context.Context, _ string, _ time.Duration) (
}

// ShareUpload - share upload not implemented for filesystem.
func (f *fsClient) ShareUpload(_ context.Context, _ bool, _ time.Duration, _ string) (string, map[string]string, *probe.Error) {
func (f *fsClient) ShareUpload(_ context.Context, _ bool, _ time.Duration, _ string, _ int64, _ int64) (string, map[string]string, *probe.Error) {
return "", nil, probe.NewError(APINotImplemented{
API: "ShareUpload",
APIType: "filesystem",
Expand Down
7 changes: 6 additions & 1 deletion cmd/client-s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -2445,7 +2445,7 @@ func (c *S3Client) ShareDownload(ctx context.Context, versionID string, expires
}

// ShareUpload - get data for presigned post http form upload.
func (c *S3Client) ShareUpload(ctx context.Context, isRecursive bool, expires time.Duration, contentType string) (string, map[string]string, *probe.Error) {
func (c *S3Client) ShareUpload(ctx context.Context, isRecursive bool, expires time.Duration, contentType string, minLen int64, maxLen int64) (string, map[string]string, *probe.Error) {
bucket, object := c.url2BucketAndObject()
p := minio.NewPostPolicy()
if e := p.SetExpires(UTCNow().Add(expires)); e != nil {
Expand All @@ -2467,6 +2467,11 @@ func (c *S3Client) ShareUpload(ctx context.Context, isRecursive bool, expires ti
return "", nil, probe.NewError(e)
}
}
if minLen != 0 || maxLen != 0 {
if e := p.SetContentLengthRange(minLen, maxLen); e != nil {
return "", nil, probe.NewError(e)
}
}
u, m, e := c.api.PresignedPostPolicy(ctx, p)
if e != nil {
return "", nil, probe.NewError(e)
Expand Down
2 changes: 1 addition & 1 deletion cmd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ type Client interface {

// I/O operations with expiration
ShareDownload(ctx context.Context, versionID string, expires time.Duration) (string, *probe.Error)
ShareUpload(context.Context, bool, time.Duration, string) (string, map[string]string, *probe.Error)
ShareUpload(context.Context, bool, time.Duration, string, int64, int64) (string, map[string]string, *probe.Error)

// Watch events
Watch(ctx context.Context, options WatchOptions) (*WatchObject, *probe.Error)
Expand Down
22 changes: 18 additions & 4 deletions cmd/share-upload-main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"
"fmt"
"regexp"
"strconv"
"strings"
"time"

Expand All @@ -33,6 +34,14 @@ var shareUploadFlags = []cli.Flag{
Name: "recursive, r",
Usage: "recursively upload any object matching the prefix",
},
cli.Int64Flag{
Name: "min-len",
Usage: "minimum size in byte",
},
cli.Int64Flag{
Name: "max-len",
Usage: "maximum size in byte",
},
shareFlagExpire,
shareFlagContentType,
}
Expand Down Expand Up @@ -66,6 +75,9 @@ EXAMPLES:

4. Generate a curl command to allow upload access to any objects matching the key prefix 'backup/'. Command expires in 2 hours.
{{.Prompt}} {{.HelpName}} --recursive --expire=2h s3/backup/2007-Mar-2/backup/

5. Generate a curl command to allow upload access for a single object with min/max lengths (in bytes).
{{.Prompt}} {{.HelpName}} --min-len=1024 --max-len=8192 s3/backup/2006-Mar-1/info.txt
`,
}

Expand Down Expand Up @@ -150,16 +162,16 @@ func saveSharedURL(objectURL, shareURL string, expiry time.Duration, contentType
}

// doShareUploadURL uploads files to the target.
func doShareUploadURL(ctx context.Context, objectURL string, isRecursive bool, expiry time.Duration, contentType string) *probe.Error {
func doShareUploadURL(ctx context.Context, objectURL string, isRecursive bool, expiry time.Duration, contentType string, minLen int64, maxLen int64) *probe.Error {
clnt, err := newClient(objectURL)
if err != nil {
return err.Trace(objectURL)
}

// Generate pre-signed access info.
shareURL, uploadInfo, err := clnt.ShareUpload(ctx, isRecursive, expiry, contentType)
shareURL, uploadInfo, err := clnt.ShareUpload(ctx, isRecursive, expiry, contentType, minLen, maxLen)
if err != nil {
return err.Trace(objectURL, "expiry="+expiry.String(), "contentType="+contentType)
return err.Trace(objectURL, "expiry="+expiry.String(), "contentType="+contentType, "minLen="+strconv.FormatInt(minLen, 10), "maxLen="+strconv.FormatInt(maxLen, 10))
}

// Get the new expanded url.
Expand Down Expand Up @@ -201,14 +213,16 @@ func mainShareUpload(cliCtx *cli.Context) error {
expireArg := cliCtx.String("expire")
expiry := shareDefaultExpiry
contentType := cliCtx.String("content-type")
minLen := cliCtx.Int64("min-len")
maxLen := cliCtx.Int64("max-len")
if expireArg != "" {
var e error
expiry, e = time.ParseDuration(expireArg)
fatalIf(probe.NewError(e), "Unable to parse expire=`"+expireArg+"`.")
}

for _, targetURL := range cliCtx.Args() {
err := doShareUploadURL(ctx, targetURL, isRecursive, expiry, contentType)
err := doShareUploadURL(ctx, targetURL, isRecursive, expiry, contentType, minLen, maxLen)
if err != nil {
switch err.ToGoError().(type) {
case APINotImplemented:
Expand Down