You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

api-get-options.go 3.8KB

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