From b1ca6482d53df55782793e5e4312611df4174072 Mon Sep 17 00:00:00 2001 From: Anis Elleuch Date: Fri, 21 Aug 2020 08:08:55 +0100 Subject: [PATCH] share: Support --version-id in Download command (#3365) --- cmd/client-fs.go | 2 +- cmd/client-s3.go | 5 ++++- cmd/client.go | 2 +- cmd/find.go | 2 +- cmd/share-db-v1.go | 1 + cmd/share-download-main.go | 22 +++++++++++++++++----- 6 files changed, 25 insertions(+), 9 deletions(-) diff --git a/cmd/client-fs.go b/cmd/client-fs.go index 42e66c6d..4cf8e56e 100644 --- a/cmd/client-fs.go +++ b/cmd/client-fs.go @@ -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", diff --git a/cmd/client-s3.go b/cmd/client-s3.go index b9209c1d..e1d5577c 100644 --- a/cmd/client-s3.go +++ b/cmd/client-s3.go @@ -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) diff --git a/cmd/client.go b/cmd/client.go index efd15423..bfc3f3da 100644 --- a/cmd/client.go +++ b/cmd/client.go @@ -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 diff --git a/cmd/find.go b/cmd/find.go index 43a356dd..99a2231d 100644 --- a/cmd/find.go +++ b/cmd/find.go @@ -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 diff --git a/cmd/share-db-v1.go b/cmd/share-db-v1.go index 531f8fa5..0dcb8c5c 100644 --- a/cmd/share-db-v1.go +++ b/cmd/share-db-v1.go @@ -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. diff --git a/cmd/share-download-main.go b/cmd/share-download-main.go index 5dd10c74..0767f11b 100644 --- a/cmd/share-download-main.go +++ b/cmd/share-download-main.go @@ -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: