option.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. package oss
  2. import (
  3. "bytes"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "sort"
  8. "strconv"
  9. "time"
  10. )
  11. type optionType string
  12. const (
  13. optionParam optionType = "parameter" // URL中的参数
  14. optionHTTP optionType = "http" // HTTP头
  15. )
  16. type (
  17. optionValue struct {
  18. Value string
  19. Type optionType
  20. }
  21. // Option http option
  22. Option func(map[string]optionValue) error
  23. )
  24. // ACL is an option to set X-Oss-Acl header
  25. func ACL(acl ACLType) Option {
  26. return setHeader(HTTPHeaderOssACL, string(acl))
  27. }
  28. // ContentType is an option to set Content-Type header
  29. func ContentType(value string) Option {
  30. return setHeader(HTTPHeaderContentType, value)
  31. }
  32. // ContentLength is an option to set Content-Length header
  33. func ContentLength(length int64) Option {
  34. return setHeader(HTTPHeaderContentLength, strconv.FormatInt(length, 10))
  35. }
  36. // CacheControl is an option to set Cache-Control header
  37. func CacheControl(value string) Option {
  38. return setHeader(HTTPHeaderCacheControl, value)
  39. }
  40. // ContentDisposition is an option to set Content-Disposition header
  41. func ContentDisposition(value string) Option {
  42. return setHeader(HTTPHeaderContentDisposition, value)
  43. }
  44. // ContentEncoding is an option to set Content-Encoding header
  45. func ContentEncoding(value string) Option {
  46. return setHeader(HTTPHeaderContentEncoding, value)
  47. }
  48. // Expires is an option to set Expires header
  49. func Expires(t time.Time) Option {
  50. return setHeader(HTTPHeaderExpires, t.Format(http.TimeFormat))
  51. }
  52. // Meta is an option to set Meta header
  53. func Meta(key, value string) Option {
  54. return setHeader(HTTPHeaderOssMetaPrefix+key, value)
  55. }
  56. // Range is an option to set Range header, [start, end]
  57. func Range(start, end int64) Option {
  58. return setHeader(HTTPHeaderRange, fmt.Sprintf("bytes=%d-%d", start, end))
  59. }
  60. // AcceptEncoding is an option to set Accept-Encoding header
  61. func AcceptEncoding(value string) Option {
  62. return setHeader(HTTPHeaderAcceptEncoding, value)
  63. }
  64. // IfModifiedSince is an option to set If-Modified-Since header
  65. func IfModifiedSince(t time.Time) Option {
  66. return setHeader(HTTPHeaderIfModifiedSince, t.Format(http.TimeFormat))
  67. }
  68. // IfUnmodifiedSince is an option to set If-Unmodified-Since header
  69. func IfUnmodifiedSince(t time.Time) Option {
  70. return setHeader(HTTPHeaderIfUnmodifiedSince, t.Format(http.TimeFormat))
  71. }
  72. // IfMatch is an option to set If-Match header
  73. func IfMatch(value string) Option {
  74. return setHeader(HTTPHeaderIfMatch, value)
  75. }
  76. // IfNoneMatch is an option to set IfNoneMatch header
  77. func IfNoneMatch(value string) Option {
  78. return setHeader(HTTPHeaderIfNoneMatch, value)
  79. }
  80. // CopySource is an option to set X-Oss-Copy-Source header
  81. func CopySource(sourceBucket, sourceObject string) Option {
  82. return setHeader(HTTPHeaderOssCopySource, "/"+sourceBucket+"/"+sourceObject)
  83. }
  84. // CopySourceRange is an option to set X-Oss-Copy-Source header
  85. func CopySourceRange(startPosition, partSize int64) Option {
  86. val := "bytes=" + strconv.FormatInt(startPosition, 10) + "-" +
  87. strconv.FormatInt((startPosition+partSize-1), 10)
  88. return setHeader(HTTPHeaderOssCopySourceRange, val)
  89. }
  90. // CopySourceIfMatch is an option to set X-Oss-Copy-Source-If-Match header
  91. func CopySourceIfMatch(value string) Option {
  92. return setHeader(HTTPHeaderOssCopySourceIfMatch, value)
  93. }
  94. // CopySourceIfNoneMatch is an option to set X-Oss-Copy-Source-If-None-Match header
  95. func CopySourceIfNoneMatch(value string) Option {
  96. return setHeader(HTTPHeaderOssCopySourceIfNoneMatch, value)
  97. }
  98. // CopySourceIfModifiedSince is an option to set X-Oss-CopySource-If-Modified-Since header
  99. func CopySourceIfModifiedSince(t time.Time) Option {
  100. return setHeader(HTTPHeaderOssCopySourceIfModifiedSince, t.Format(http.TimeFormat))
  101. }
  102. // CopySourceIfUnmodifiedSince is an option to set X-Oss-Copy-Source-If-Unmodified-Since header
  103. func CopySourceIfUnmodifiedSince(t time.Time) Option {
  104. return setHeader(HTTPHeaderOssCopySourceIfUnmodifiedSince, t.Format(http.TimeFormat))
  105. }
  106. // MetadataDirective is an option to set X-Oss-Metadata-Directive header
  107. func MetadataDirective(directive MetadataDirectiveType) Option {
  108. return setHeader(HTTPHeaderOssMetadataDirective, string(directive))
  109. }
  110. // ServerSideEncryption is an option to set X-Oss-Server-Side-Encryption header
  111. func ServerSideEncryption(value string) Option {
  112. return setHeader(HTTPHeaderOssServerSideEncryption, value)
  113. }
  114. // ObjectACL is an option to set X-Oss-Object-Acl header
  115. func ObjectACL(acl ACLType) Option {
  116. return setHeader(HTTPHeaderOssObjectACL, string(acl))
  117. }
  118. // Origin is an option to set Origin header
  119. func Origin(value string) Option {
  120. return setHeader(HTTPHeaderOrigin, value)
  121. }
  122. // Delimiter is an option to set delimiler parameter
  123. func Delimiter(value string) Option {
  124. return addParam("delimiter", value)
  125. }
  126. // Marker is an option to set marker parameter
  127. func Marker(value string) Option {
  128. return addParam("marker", value)
  129. }
  130. // MaxKeys is an option to set maxkeys parameter
  131. func MaxKeys(value int) Option {
  132. return addParam("max-keys", strconv.Itoa(value))
  133. }
  134. // Prefix is an option to set prefix parameter
  135. func Prefix(value string) Option {
  136. return addParam("prefix", value)
  137. }
  138. // EncodingType is an option to set encoding-type parameter
  139. func EncodingType(value string) Option {
  140. return addParam("encoding-type", value)
  141. }
  142. // MaxUploads is an option to set max-uploads parameter
  143. func MaxUploads(value int) Option {
  144. return addParam("max-uploads", strconv.Itoa(value))
  145. }
  146. // KeyMarker is an option to set key-marker parameter
  147. func KeyMarker(value string) Option {
  148. return addParam("key-marker", value)
  149. }
  150. // UploadIDMarker is an option to set upload-id-marker parameter
  151. func UploadIDMarker(value string) Option {
  152. return addParam("upload-id-marker", value)
  153. }
  154. const deleteObjectsQuiet = "delete-objects-quiet"
  155. // DeleteObjectsQuiet DeleteObjects详细(verbose)模式或简单(quiet)模式,默认是详细模式。
  156. func DeleteObjectsQuiet(isQuiet bool) Option {
  157. return addParam(deleteObjectsQuiet, strconv.FormatBool(isQuiet))
  158. }
  159. func setHeader(key, value string) Option {
  160. return func(args map[string]optionValue) error {
  161. if value == "" {
  162. return nil
  163. }
  164. args[key] = optionValue{value, optionHTTP}
  165. return nil
  166. }
  167. }
  168. func addParam(key, value string) Option {
  169. return func(args map[string]optionValue) error {
  170. if value == "" {
  171. return nil
  172. }
  173. args[key] = optionValue{value, optionParam}
  174. return nil
  175. }
  176. }
  177. func handleOptions(headers map[string]string, options []Option) error {
  178. args := map[string]optionValue{}
  179. for _, option := range options {
  180. if option != nil {
  181. if err := option(args); err != nil {
  182. return err
  183. }
  184. }
  185. }
  186. for k, v := range args {
  187. if v.Type == optionHTTP {
  188. headers[k] = v.Value
  189. }
  190. }
  191. return nil
  192. }
  193. func handleParams(options []Option) (string, error) {
  194. // option
  195. args := map[string]optionValue{}
  196. for _, option := range options {
  197. if option != nil {
  198. if err := option(args); err != nil {
  199. return "", err
  200. }
  201. }
  202. }
  203. // sort
  204. var buf bytes.Buffer
  205. keys := make([]string, 0, len(args))
  206. for k, v := range args {
  207. if v.Type == optionParam {
  208. keys = append(keys, k)
  209. }
  210. }
  211. sort.Strings(keys)
  212. // serialize
  213. for _, k := range keys {
  214. vs := args[k]
  215. prefix := url.QueryEscape(k) + "="
  216. if buf.Len() > 0 {
  217. buf.WriteByte('&')
  218. }
  219. buf.WriteString(prefix)
  220. buf.WriteString(url.QueryEscape(vs.Value))
  221. }
  222. return buf.String(), nil
  223. }
  224. func findOption(options []Option, param, defaultVal string) (string, error) {
  225. args := map[string]optionValue{}
  226. for _, option := range options {
  227. if option != nil {
  228. if err := option(args); err != nil {
  229. return "", err
  230. }
  231. }
  232. }
  233. if val, ok := args[param]; ok {
  234. return val.Value, nil
  235. }
  236. return defaultVal, nil
  237. }