github.com/minio/minio-go/v6@v6.0.57/api-get-options.go (about)

     1  /*
     2   * MinIO Go Library for Amazon S3 Compatible Cloud Storage
     3   * Copyright 2015-2017 MinIO, Inc.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package minio
    19  
    20  import (
    21  	"fmt"
    22  	"net/http"
    23  	"time"
    24  
    25  	"github.com/minio/minio-go/v6/pkg/encrypt"
    26  )
    27  
    28  // GetObjectOptions are used to specify additional headers or options
    29  // during GET requests.
    30  type GetObjectOptions struct {
    31  	headers              map[string]string
    32  	ServerSideEncryption encrypt.ServerSide
    33  }
    34  
    35  // StatObjectOptions are used to specify additional headers or options
    36  // during GET info/stat requests.
    37  type StatObjectOptions struct {
    38  	GetObjectOptions
    39  }
    40  
    41  // Header returns the http.Header representation of the GET options.
    42  func (o GetObjectOptions) Header() http.Header {
    43  	headers := make(http.Header, len(o.headers))
    44  	for k, v := range o.headers {
    45  		headers.Set(k, v)
    46  	}
    47  	if o.ServerSideEncryption != nil && o.ServerSideEncryption.Type() == encrypt.SSEC {
    48  		o.ServerSideEncryption.Marshal(headers)
    49  	}
    50  	return headers
    51  }
    52  
    53  // Set adds a key value pair to the options. The
    54  // key-value pair will be part of the HTTP GET request
    55  // headers.
    56  func (o *GetObjectOptions) Set(key, value string) {
    57  	if o.headers == nil {
    58  		o.headers = make(map[string]string)
    59  	}
    60  	o.headers[http.CanonicalHeaderKey(key)] = value
    61  }
    62  
    63  // SetMatchETag - set match etag.
    64  func (o *GetObjectOptions) SetMatchETag(etag string) error {
    65  	if etag == "" {
    66  		return ErrInvalidArgument("ETag cannot be empty.")
    67  	}
    68  	o.Set("If-Match", "\""+etag+"\"")
    69  	return nil
    70  }
    71  
    72  // SetMatchETagExcept - set match etag except.
    73  func (o *GetObjectOptions) SetMatchETagExcept(etag string) error {
    74  	if etag == "" {
    75  		return ErrInvalidArgument("ETag cannot be empty.")
    76  	}
    77  	o.Set("If-None-Match", "\""+etag+"\"")
    78  	return nil
    79  }
    80  
    81  // SetUnmodified - set unmodified time since.
    82  func (o *GetObjectOptions) SetUnmodified(modTime time.Time) error {
    83  	if modTime.IsZero() {
    84  		return ErrInvalidArgument("Modified since cannot be empty.")
    85  	}
    86  	o.Set("If-Unmodified-Since", modTime.Format(http.TimeFormat))
    87  	return nil
    88  }
    89  
    90  // SetModified - set modified time since.
    91  func (o *GetObjectOptions) SetModified(modTime time.Time) error {
    92  	if modTime.IsZero() {
    93  		return ErrInvalidArgument("Modified since cannot be empty.")
    94  	}
    95  	o.Set("If-Modified-Since", modTime.Format(http.TimeFormat))
    96  	return nil
    97  }
    98  
    99  // SetRange - set the start and end offset of the object to be read.
   100  // See https://tools.ietf.org/html/rfc7233#section-3.1 for reference.
   101  func (o *GetObjectOptions) SetRange(start, end int64) error {
   102  	switch {
   103  	case start == 0 && end < 0:
   104  		// Read last '-end' bytes. `bytes=-N`.
   105  		o.Set("Range", fmt.Sprintf("bytes=%d", end))
   106  	case 0 < start && end == 0:
   107  		// Read everything starting from offset
   108  		// 'start'. `bytes=N-`.
   109  		o.Set("Range", fmt.Sprintf("bytes=%d-", start))
   110  	case 0 <= start && start <= end:
   111  		// Read everything starting at 'start' till the
   112  		// 'end'. `bytes=N-M`
   113  		o.Set("Range", fmt.Sprintf("bytes=%d-%d", start, end))
   114  	default:
   115  		// All other cases such as
   116  		// bytes=-3-
   117  		// bytes=5-3
   118  		// bytes=-2-4
   119  		// bytes=-3-0
   120  		// bytes=-3--2
   121  		// are invalid.
   122  		return ErrInvalidArgument(
   123  			fmt.Sprintf(
   124  				"Invalid range specified: start=%d end=%d",
   125  				start, end))
   126  	}
   127  	return nil
   128  }