option.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. package oss
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strconv"
  6. "strings"
  7. "time"
  8. )
  9. type optionType string
  10. const (
  11. optionParam optionType = "HTTPParameter" // URL参数
  12. optionHTTP optionType = "HTTPHeader" // HTTP头
  13. optionArg optionType = "FuncArgument" // 函数参数
  14. )
  15. const (
  16. deleteObjectsQuiet = "delete-objects-quiet"
  17. routineNum = "x-routine-num"
  18. checkpointConfig = "x-cp-config"
  19. initCRC64 = "init-crc64"
  20. progressListener = "x-progress-listener"
  21. storageClass = "storage-class"
  22. )
  23. type (
  24. optionValue struct {
  25. Value interface{}
  26. Type optionType
  27. }
  28. // Option http option
  29. Option func(map[string]optionValue) error
  30. )
  31. // ACL is an option to set X-Oss-Acl header
  32. func ACL(acl ACLType) Option {
  33. return setHeader(HTTPHeaderOssACL, string(acl))
  34. }
  35. // ContentType is an option to set Content-Type header
  36. func ContentType(value string) Option {
  37. return setHeader(HTTPHeaderContentType, value)
  38. }
  39. // ContentLength is an option to set Content-Length header
  40. func ContentLength(length int64) Option {
  41. return setHeader(HTTPHeaderContentLength, strconv.FormatInt(length, 10))
  42. }
  43. // CacheControl is an option to set Cache-Control header
  44. func CacheControl(value string) Option {
  45. return setHeader(HTTPHeaderCacheControl, value)
  46. }
  47. // ContentDisposition is an option to set Content-Disposition header
  48. func ContentDisposition(value string) Option {
  49. return setHeader(HTTPHeaderContentDisposition, value)
  50. }
  51. // ContentEncoding is an option to set Content-Encoding header
  52. func ContentEncoding(value string) Option {
  53. return setHeader(HTTPHeaderContentEncoding, value)
  54. }
  55. // ContentMD5 is an option to set Content-MD5 header
  56. func ContentMD5(value string) Option {
  57. return setHeader(HTTPHeaderContentMD5, value)
  58. }
  59. // Expires is an option to set Expires header
  60. func Expires(t time.Time) Option {
  61. return setHeader(HTTPHeaderExpires, t.Format(http.TimeFormat))
  62. }
  63. // Meta is an option to set Meta header
  64. func Meta(key, value string) Option {
  65. return setHeader(HTTPHeaderOssMetaPrefix+key, value)
  66. }
  67. // Range is an option to set Range header, [start, end]
  68. func Range(start, end int64) Option {
  69. return setHeader(HTTPHeaderRange, fmt.Sprintf("bytes=%d-%d", start, end))
  70. }
  71. // NormalizedRange is an option to set Range header, such as 1024-2048 or 1024- or -2048
  72. func NormalizedRange(nr string) Option {
  73. return setHeader(HTTPHeaderRange, fmt.Sprintf("bytes=%s", strings.TrimSpace(nr)))
  74. }
  75. // AcceptEncoding is an option to set Accept-Encoding header
  76. func AcceptEncoding(value string) Option {
  77. return setHeader(HTTPHeaderAcceptEncoding, value)
  78. }
  79. // IfModifiedSince is an option to set If-Modified-Since header
  80. func IfModifiedSince(t time.Time) Option {
  81. return setHeader(HTTPHeaderIfModifiedSince, t.Format(http.TimeFormat))
  82. }
  83. // IfUnmodifiedSince is an option to set If-Unmodified-Since header
  84. func IfUnmodifiedSince(t time.Time) Option {
  85. return setHeader(HTTPHeaderIfUnmodifiedSince, t.Format(http.TimeFormat))
  86. }
  87. // IfMatch is an option to set If-Match header
  88. func IfMatch(value string) Option {
  89. return setHeader(HTTPHeaderIfMatch, value)
  90. }
  91. // IfNoneMatch is an option to set IfNoneMatch header
  92. func IfNoneMatch(value string) Option {
  93. return setHeader(HTTPHeaderIfNoneMatch, value)
  94. }
  95. // CopySource is an option to set X-Oss-Copy-Source header
  96. func CopySource(sourceBucket, sourceObject string) Option {
  97. return setHeader(HTTPHeaderOssCopySource, "/"+sourceBucket+"/"+sourceObject)
  98. }
  99. // CopySourceRange is an option to set X-Oss-Copy-Source header
  100. func CopySourceRange(startPosition, partSize int64) Option {
  101. val := "bytes=" + strconv.FormatInt(startPosition, 10) + "-" +
  102. strconv.FormatInt((startPosition+partSize-1), 10)
  103. return setHeader(HTTPHeaderOssCopySourceRange, val)
  104. }
  105. // CopySourceIfMatch is an option to set X-Oss-Copy-Source-If-Match header
  106. func CopySourceIfMatch(value string) Option {
  107. return setHeader(HTTPHeaderOssCopySourceIfMatch, value)
  108. }
  109. // CopySourceIfNoneMatch is an option to set X-Oss-Copy-Source-If-None-Match header
  110. func CopySourceIfNoneMatch(value string) Option {
  111. return setHeader(HTTPHeaderOssCopySourceIfNoneMatch, value)
  112. }
  113. // CopySourceIfModifiedSince is an option to set X-Oss-CopySource-If-Modified-Since header
  114. func CopySourceIfModifiedSince(t time.Time) Option {
  115. return setHeader(HTTPHeaderOssCopySourceIfModifiedSince, t.Format(http.TimeFormat))
  116. }
  117. // CopySourceIfUnmodifiedSince is an option to set X-Oss-Copy-Source-If-Unmodified-Since header
  118. func CopySourceIfUnmodifiedSince(t time.Time) Option {
  119. return setHeader(HTTPHeaderOssCopySourceIfUnmodifiedSince, t.Format(http.TimeFormat))
  120. }
  121. // MetadataDirective is an option to set X-Oss-Metadata-Directive header
  122. func MetadataDirective(directive MetadataDirectiveType) Option {
  123. return setHeader(HTTPHeaderOssMetadataDirective, string(directive))
  124. }
  125. // ServerSideEncryption is an option to set X-Oss-Server-Side-Encryption header
  126. func ServerSideEncryption(value string) Option {
  127. return setHeader(HTTPHeaderOssServerSideEncryption, value)
  128. }
  129. // ObjectACL is an option to set X-Oss-Object-Acl header
  130. func ObjectACL(acl ACLType) Option {
  131. return setHeader(HTTPHeaderOssObjectACL, string(acl))
  132. }
  133. // symlinkTarget is an option to set X-Oss-Symlink-Target
  134. func symlinkTarget(targetObjectKey string) Option {
  135. return setHeader(HTTPHeaderOssSymlinkTarget, targetObjectKey)
  136. }
  137. // Origin is an option to set Origin header
  138. func Origin(value string) Option {
  139. return setHeader(HTTPHeaderOrigin, value)
  140. }
  141. // Delimiter is an option to set delimiler parameter
  142. func Delimiter(value string) Option {
  143. return addParam("delimiter", value)
  144. }
  145. // Marker is an option to set marker parameter
  146. func Marker(value string) Option {
  147. return addParam("marker", value)
  148. }
  149. // MaxKeys is an option to set maxkeys parameter
  150. func MaxKeys(value int) Option {
  151. return addParam("max-keys", strconv.Itoa(value))
  152. }
  153. // Prefix is an option to set prefix parameter
  154. func Prefix(value string) Option {
  155. return addParam("prefix", value)
  156. }
  157. // EncodingType is an option to set encoding-type parameter
  158. func EncodingType(value string) Option {
  159. return addParam("encoding-type", value)
  160. }
  161. // MaxUploads is an option to set max-uploads parameter
  162. func MaxUploads(value int) Option {
  163. return addParam("max-uploads", strconv.Itoa(value))
  164. }
  165. // KeyMarker is an option to set key-marker parameter
  166. func KeyMarker(value string) Option {
  167. return addParam("key-marker", value)
  168. }
  169. // UploadIDMarker is an option to set upload-id-marker parameter
  170. func UploadIDMarker(value string) Option {
  171. return addParam("upload-id-marker", value)
  172. }
  173. // DeleteObjectsQuiet DeleteObjects详细(verbose)模式或简单(quiet)模式,默认详细模式。
  174. func DeleteObjectsQuiet(isQuiet bool) Option {
  175. return addArg(deleteObjectsQuiet, isQuiet)
  176. }
  177. // StorageClass bucket的存储方式
  178. func StorageClass(value StorageClassType) Option {
  179. return addArg(storageClass, value)
  180. }
  181. // 断点续传配置,包括是否启用、cp文件
  182. type cpConfig struct {
  183. IsEnable bool
  184. FilePath string
  185. }
  186. // Checkpoint DownloadFile/UploadFile是否开启checkpoint及checkpoint文件路径
  187. func Checkpoint(isEnable bool, filePath string) Option {
  188. return addArg(checkpointConfig, &cpConfig{isEnable, filePath})
  189. }
  190. // Routines DownloadFile/UploadFile并发数
  191. func Routines(n int) Option {
  192. return addArg(routineNum, n)
  193. }
  194. // InitCRC AppendObject CRC的校验的初始值
  195. func InitCRC(initCRC uint64) Option {
  196. return addArg(initCRC64, initCRC)
  197. }
  198. // Progress set progress listener
  199. func Progress(listener ProgressListener) Option {
  200. return addArg(progressListener, listener)
  201. }
  202. // ResponseContentType is an option to set response-content-type param
  203. func ResponseContentType(value string) Option {
  204. return addParam("response-content-type", value)
  205. }
  206. // ResponseContentLanguage is an option to set response-content-language param
  207. func ResponseContentLanguage(value string) Option {
  208. return addParam("response-content-language", value)
  209. }
  210. // ResponseExpires is an option to set response-expires param
  211. func ResponseExpires(value string) Option {
  212. return addParam("response-expires", value)
  213. }
  214. // ResponseCacheControl is an option to set response-cache-control param
  215. func ResponseCacheControl(value string) Option {
  216. return addParam("response-cache-control", value)
  217. }
  218. // ResponseContentDisposition is an option to set response-content-disposition param
  219. func ResponseContentDisposition(value string) Option {
  220. return addParam("response-content-disposition", value)
  221. }
  222. // ResponseContentEncoding is an option to set response-content-encoding param
  223. func ResponseContentEncoding(value string) Option {
  224. return addParam("response-content-encoding", value)
  225. }
  226. // Process is an option to set X-Oss-Process param
  227. func Process(value string) Option {
  228. return addParam("X-Oss-Process", value)
  229. }
  230. func setHeader(key string, value interface{}) Option {
  231. return func(params map[string]optionValue) error {
  232. if value == nil {
  233. return nil
  234. }
  235. params[key] = optionValue{value, optionHTTP}
  236. return nil
  237. }
  238. }
  239. func addParam(key string, value interface{}) Option {
  240. return func(params map[string]optionValue) error {
  241. if value == nil {
  242. return nil
  243. }
  244. params[key] = optionValue{value, optionParam}
  245. return nil
  246. }
  247. }
  248. func addArg(key string, value interface{}) Option {
  249. return func(params map[string]optionValue) error {
  250. if value == nil {
  251. return nil
  252. }
  253. params[key] = optionValue{value, optionArg}
  254. return nil
  255. }
  256. }
  257. func handleOptions(headers map[string]string, options []Option) error {
  258. params := map[string]optionValue{}
  259. for _, option := range options {
  260. if option != nil {
  261. if err := option(params); err != nil {
  262. return err
  263. }
  264. }
  265. }
  266. for k, v := range params {
  267. if v.Type == optionHTTP {
  268. headers[k] = v.Value.(string)
  269. }
  270. }
  271. return nil
  272. }
  273. func getRawParams(options []Option) (map[string]interface{}, error) {
  274. // option
  275. params := map[string]optionValue{}
  276. for _, option := range options {
  277. if option != nil {
  278. if err := option(params); err != nil {
  279. return nil, err
  280. }
  281. }
  282. }
  283. paramsm := map[string]interface{}{}
  284. // serialize
  285. for k, v := range params {
  286. if v.Type == optionParam {
  287. vs := params[k]
  288. paramsm[k] = vs.Value.(string)
  289. }
  290. }
  291. return paramsm, nil
  292. }
  293. func findOption(options []Option, param string, defaultVal interface{}) (interface{}, error) {
  294. params := map[string]optionValue{}
  295. for _, option := range options {
  296. if option != nil {
  297. if err := option(params); err != nil {
  298. return nil, err
  299. }
  300. }
  301. }
  302. if val, ok := params[param]; ok {
  303. return val.Value, nil
  304. }
  305. return defaultVal, nil
  306. }
  307. func isOptionSet(options []Option, option string) (bool, interface{}, error) {
  308. params := map[string]optionValue{}
  309. for _, option := range options {
  310. if option != nil {
  311. if err := option(params); err != nil {
  312. return false, nil, err
  313. }
  314. }
  315. }
  316. if val, ok := params[option]; ok {
  317. return true, val.Value, nil
  318. }
  319. return false, nil, nil
  320. }