api-get-options.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. package s3cli
  18. import (
  19. "fmt"
  20. "net/http"
  21. "time"
  22. "github.com/minio/minio-go/v6/pkg/encrypt"
  23. )
  24. // GetObjectOptions are used to specify additional headers or options
  25. // during GET requests.
  26. type GetObjectOptions struct {
  27. headers map[string]string
  28. ServerSideEncryption encrypt.ServerSide
  29. }
  30. // StatObjectOptions are used to specify additional headers or options
  31. // during GET info/stat requests.
  32. type StatObjectOptions struct {
  33. GetObjectOptions
  34. }
  35. // Header returns the http.Header representation of the GET options.
  36. func (o GetObjectOptions) Header() http.Header {
  37. headers := make(http.Header, len(o.headers))
  38. for k, v := range o.headers {
  39. headers.Set(k, v)
  40. }
  41. if o.ServerSideEncryption != nil && o.ServerSideEncryption.Type() == encrypt.SSEC {
  42. o.ServerSideEncryption.Marshal(headers)
  43. }
  44. return headers
  45. }
  46. // Set adds a key value pair to the options. The
  47. // key-value pair will be part of the HTTP GET request
  48. // headers.
  49. func (o *GetObjectOptions) Set(key, value string) {
  50. if o.headers == nil {
  51. o.headers = make(map[string]string)
  52. }
  53. o.headers[http.CanonicalHeaderKey(key)] = value
  54. }
  55. // SetMatchETag - set match etag.
  56. func (o *GetObjectOptions) SetMatchETag(etag string) error {
  57. if etag == "" {
  58. return ErrInvalidArgument("ETag cannot be empty.")
  59. }
  60. o.Set("If-Match", "\""+etag+"\"")
  61. return nil
  62. }
  63. // SetMatchETagExcept - set match etag except.
  64. func (o *GetObjectOptions) SetMatchETagExcept(etag string) error {
  65. if etag == "" {
  66. return ErrInvalidArgument("ETag cannot be empty.")
  67. }
  68. o.Set("If-None-Match", "\""+etag+"\"")
  69. return nil
  70. }
  71. // SetUnmodified - set unmodified time since.
  72. func (o *GetObjectOptions) SetUnmodified(modTime time.Time) error {
  73. if modTime.IsZero() {
  74. return ErrInvalidArgument("Modified since cannot be empty.")
  75. }
  76. o.Set("If-Unmodified-Since", modTime.Format(http.TimeFormat))
  77. return nil
  78. }
  79. // SetModified - set modified time since.
  80. func (o *GetObjectOptions) SetModified(modTime time.Time) error {
  81. if modTime.IsZero() {
  82. return ErrInvalidArgument("Modified since cannot be empty.")
  83. }
  84. o.Set("If-Modified-Since", modTime.Format(http.TimeFormat))
  85. return nil
  86. }
  87. // SetRange - set the start and end offset of the object to be read.
  88. // See https://tools.ietf.org/html/rfc7233#section-3.1 for reference.
  89. func (o *GetObjectOptions) SetRange(start, end int64) error {
  90. switch {
  91. case start == 0 && end < 0:
  92. // Read last '-end' bytes. `bytes=-N`.
  93. o.Set("Range", fmt.Sprintf("bytes=%d", end))
  94. case 0 < start && end == 0:
  95. // Read everything starting from offset
  96. // 'start'. `bytes=N-`.
  97. o.Set("Range", fmt.Sprintf("bytes=%d-", start))
  98. case 0 <= start && start <= end:
  99. // Read everything starting at 'start' till the
  100. // 'end'. `bytes=N-M`
  101. o.Set("Range", fmt.Sprintf("bytes=%d-%d", start, end))
  102. default:
  103. // All other cases such as
  104. // bytes=-3-
  105. // bytes=5-3
  106. // bytes=-2-4
  107. // bytes=-3-0
  108. // bytes=-3--2
  109. // are invalid.
  110. return ErrInvalidArgument(
  111. fmt.Sprintf(
  112. "Invalid range specified: start=%d end=%d",
  113. start, end))
  114. }
  115. return nil
  116. }