share: Support --version-id in Download command (#3365)

This commit is contained in:
Anis Elleuch
2020-08-21 00:08:55 -07:00
committed by GitHub
parent 54fab0ad8f
commit b1ca6482d5
6 changed files with 25 additions and 9 deletions
+1 -1
View File
@@ -406,7 +406,7 @@ func (f *fsClient) Put(ctx context.Context, reader io.Reader, size int64, metada
}
// ShareDownload - share download not implemented for filesystem.
func (f *fsClient) ShareDownload(ctx context.Context, expires time.Duration) (string, *probe.Error) {
func (f *fsClient) ShareDownload(ctx context.Context, versionID string, expires time.Duration) (string, *probe.Error) {
return "", probe.NewError(APINotImplemented{
API: "ShareDownload",
APIType: "filesystem",
+4 -1
View File
@@ -2220,10 +2220,13 @@ func (c *S3Client) listRecursiveInRoutine(ctx context.Context, contentCh chan *C
}
// ShareDownload - get a usable presigned object url to share.
func (c *S3Client) ShareDownload(ctx context.Context, expires time.Duration) (string, *probe.Error) {
func (c *S3Client) ShareDownload(ctx context.Context, versionID string, expires time.Duration) (string, *probe.Error) {
bucket, object := c.url2BucketAndObject()
// No additional request parameters are set for the time being.
reqParams := make(url.Values)
if versionID != "" {
reqParams.Set("versionId", versionID)
}
presignedURL, e := c.api.PresignedGetObject(ctx, bucket, object, expires, reqParams)
if e != nil {
return "", probe.NewError(e)
+1 -1
View File
@@ -114,7 +114,7 @@ type Client interface {
PutObjectLegalHold(ctx context.Context, hold minio.LegalHoldStatus) *probe.Error
// I/O operations with expiration
ShareDownload(ctx context.Context, expires time.Duration) (string, *probe.Error)
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)
// Watch events
+1 -1
View File
@@ -435,7 +435,7 @@ func getShareURL(ctx context.Context, path string) string {
fatalIf(err.Trace(targetAlias, objectURL), "Unable to initialize new client from alias.")
// Set default expiry for each url (point of no longer valid), to be 7 days
shareURL, err := newClnt.ShareDownload(ctx, defaultSevenDays)
shareURL, err := newClnt.ShareDownload(ctx, "", defaultSevenDays)
fatalIf(err.Trace(targetAlias, objectURL), "Unable to generate share url.")
return shareURL
+1
View File
@@ -28,6 +28,7 @@ import (
// shareEntryV1 - container for each download/upload entries.
type shareEntryV1 struct {
URL string `json:"share"` // Object URL.
VersionID string `json:"versionID"`
Date time.Time `json:"date"`
Expiry time.Duration `json:"expiry"`
ContentType string `json:"contentType,omitempty"` // Only used by upload cmd.
+17 -5
View File
@@ -31,6 +31,10 @@ var (
Name: "recursive, r",
Usage: "share all objects recursively",
},
cli.StringFlag{
Name: "version-id",
Usage: "share a particular object version",
},
shareFlagExpire,
}
)
@@ -90,8 +94,14 @@ func checkShareDownloadSyntax(ctx context.Context, cliCtx *cli.Context, encKeyDB
fatalIf(errDummy().Trace(expiry.String()), "Expiry cannot be larger than 7 days.")
}
// Validate if object exists only if the `--recursive` flag was NOT specified
isRecursive := cliCtx.Bool("recursive")
versionID := cliCtx.String("version-id")
if versionID != "" && isRecursive {
fatalIf(errDummy().Trace(), "--version-id cannot be specified with --recursive flag.")
}
// Validate if object exists only if the `--recursive` flag was NOT specified
if !isRecursive {
for _, url := range cliCtx.Args() {
_, _, err := url2Stat(ctx, url, "", false, encKeyDB, time.Time{})
@@ -103,7 +113,7 @@ func checkShareDownloadSyntax(ctx context.Context, cliCtx *cli.Context, encKeyDB
}
// doShareURL share files from target.
func doShareDownloadURL(ctx context.Context, targetURL string, isRecursive bool, expiry time.Duration) *probe.Error {
func doShareDownloadURL(ctx context.Context, targetURL, versionID string, isRecursive bool, expiry time.Duration) *probe.Error {
targetAlias, targetURLFull, _, err := expandAlias(targetURL)
if err != nil {
return err.Trace(targetURL)
@@ -124,7 +134,7 @@ func doShareDownloadURL(ctx context.Context, targetURL string, isRecursive bool,
// Channel which will receive objects whose URLs need to be shared
objectsCh := make(chan *ClientContent)
content, err := clnt.Stat(ctx, StatOptions{})
content, err := clnt.Stat(ctx, StatOptions{versionID: versionID})
if err != nil {
return err.Trace(clnt.GetURL().String())
}
@@ -161,13 +171,14 @@ func doShareDownloadURL(ctx context.Context, targetURL string, isRecursive bool,
continue
}
objectURL := content.URL.String()
objectVersionID := content.VersionID
newClnt, err := newClientFromAlias(targetAlias, objectURL)
if err != nil {
return err.Trace(objectURL)
}
// Generate share URL.
shareURL, err := newClnt.ShareDownload(ctx, expiry)
shareURL, err := newClnt.ShareDownload(ctx, objectVersionID, expiry)
if err != nil {
// add objectURL and expiry as part of the trace arguments.
return err.Trace(objectURL, "expiry="+expiry.String())
@@ -208,6 +219,7 @@ func mainShareDownload(cliCtx *cli.Context) error {
// Set command flags from context.
isRecursive := cliCtx.Bool("recursive")
versionID := cliCtx.String("version-id")
expiry := shareDefaultExpiry
if cliCtx.String("expire") != "" {
var e error
@@ -216,7 +228,7 @@ func mainShareDownload(cliCtx *cli.Context) error {
}
for _, targetURL := range cliCtx.Args() {
err := doShareDownloadURL(ctx, targetURL, isRecursive, expiry)
err := doShareDownloadURL(ctx, targetURL, versionID, isRecursive, expiry)
if err != nil {
switch err.ToGoError().(type) {
case APINotImplemented: