Skip to content

Commit

Permalink
Feat(minio#1755):
Browse files Browse the repository at this point in the history
Support passing in a URL query string in GetObjectOptions.
  • Loading branch information
reedchan7 committed Jan 14, 2023
1 parent b376406 commit 028eb52
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -2,4 +2,5 @@
*.test
validator
golangci-lint
functional_tests
functional_tests
.idea
14 changes: 2 additions & 12 deletions api-get-object.go
Expand Up @@ -23,8 +23,6 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"sync"

"github.com/minio/minio-go/v7/pkg/s3utils"
Expand Down Expand Up @@ -640,7 +638,7 @@ func newObject(ctx context.Context, cancel context.CancelFunc, reqCh chan<- getR

// getObject - retrieve object from Object Storage.
//
// Additionally this function also takes range arguments to download the specified
// Additionally, this function also takes range arguments to download the specified
// range bytes of an object. Setting offset and length = 0 will download the full object.
//
// For more information about the HTTP Range header.
Expand All @@ -654,19 +652,11 @@ func (c *Client) getObject(ctx context.Context, bucketName, objectName string, o
return nil, ObjectInfo{}, nil, err
}

urlValues := make(url.Values)
if opts.VersionID != "" {
urlValues.Set("versionId", opts.VersionID)
}
if opts.PartNumber > 0 {
urlValues.Set("partNumber", strconv.Itoa(opts.PartNumber))
}

// Execute GET on objectName.
resp, err := c.executeMethod(ctx, http.MethodGet, requestMetadata{
bucketName: bucketName,
objectName: objectName,
queryValues: urlValues,
queryValues: opts.ToQueryValues(),
customHeader: opts.Header(),
contentSHA256Hex: emptySHA256Hex,
})
Expand Down
42 changes: 41 additions & 1 deletion api-get-options.go
Expand Up @@ -20,6 +20,8 @@ package minio
import (
"fmt"
"net/http"
"net/url"
"strconv"
"time"

"github.com/minio/minio-go/v7/pkg/encrypt"
Expand All @@ -36,6 +38,7 @@ type AdvancedGetOptions struct {
// during GET requests.
type GetObjectOptions struct {
headers map[string]string
reqParams url.Values
ServerSideEncryption encrypt.ServerSide
VersionID string
PartNumber int
Expand All @@ -54,7 +57,7 @@ type GetObjectOptions struct {
type StatObjectOptions = GetObjectOptions

// Header returns the http.Header representation of the GET options.
func (o GetObjectOptions) Header() http.Header {
func (o *GetObjectOptions) Header() http.Header {
headers := make(http.Header, len(o.headers))
for k, v := range o.headers {
headers.Set(k, v)
Expand Down Expand Up @@ -83,6 +86,22 @@ func (o *GetObjectOptions) Set(key, value string) {
o.headers[http.CanonicalHeaderKey(key)] = value
}

// SetReqParam - set request query string parameter
func (o *GetObjectOptions) SetReqParam(key, value string) {
if o.reqParams == nil {
o.reqParams = make(url.Values)
}
o.reqParams.Set(key, value)
}

// AddReqParam - add request query string parameter
func (o *GetObjectOptions) AddReqParam(key, value string) {
if o.reqParams == nil {
o.reqParams = make(url.Values)
}
o.reqParams.Add(key, value)
}

// SetMatchETag - set match etag.
func (o *GetObjectOptions) SetMatchETag(etag string) error {
if etag == "" {
Expand Down Expand Up @@ -149,3 +168,24 @@ func (o *GetObjectOptions) SetRange(start, end int64) error {
}
return nil
}

// ToQueryValues - Convert the versionId, partNumber, and reqParams in Options to query string parameters.
func (o *GetObjectOptions) ToQueryValues() url.Values {
urlValues := make(url.Values)
if o.VersionID != "" {
urlValues.Set("versionId", o.VersionID)
}
if o.PartNumber > 0 {
urlValues.Set("partNumber", strconv.Itoa(o.PartNumber))
}

if o.reqParams != nil {
for key, values := range o.reqParams {
for _, value := range values {
urlValues.Add(key, value)
}
}
}

return urlValues
}

0 comments on commit 028eb52

Please sign in to comment.