option.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 parameter
  12. optionHTTP optionType = "HTTPHeader" // HTTP header
  13. optionArg optionType = "FuncArgument" // Function argument
  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. // ObjectStorageClass is an option to set the storage class of object
  142. func ObjectStorageClass(storageClass StorageClassType) Option {
  143. return setHeader(HTTPHeaderOssStorageClass, string(storageClass))
  144. }
  145. // Callback is an option to set callback values
  146. func Callback(callback string) Option {
  147. return setHeader(HTTPHeaderOssCallback, callback)
  148. }
  149. // CallbackVar is an option to set callback user defined values
  150. func CallbackVar(callbackVar string) Option {
  151. return setHeader(HTTPHeaderOssCallbackVar, callbackVar)
  152. }
  153. // RequestPayer is an option to set payer who pay for the request
  154. func RequestPayer(payerType PayerType) Option {
  155. return setHeader(HTTPHeaderOSSRequester, string(payerType))
  156. }
  157. // Delimiter is an option to set delimiler parameter
  158. func Delimiter(value string) Option {
  159. return addParam("delimiter", value)
  160. }
  161. // Marker is an option to set marker parameter
  162. func Marker(value string) Option {
  163. return addParam("marker", value)
  164. }
  165. // MaxKeys is an option to set maxkeys parameter
  166. func MaxKeys(value int) Option {
  167. return addParam("max-keys", strconv.Itoa(value))
  168. }
  169. // Prefix is an option to set prefix parameter
  170. func Prefix(value string) Option {
  171. return addParam("prefix", value)
  172. }
  173. // EncodingType is an option to set encoding-type parameter
  174. func EncodingType(value string) Option {
  175. return addParam("encoding-type", value)
  176. }
  177. // MaxUploads is an option to set max-uploads parameter
  178. func MaxUploads(value int) Option {
  179. return addParam("max-uploads", strconv.Itoa(value))
  180. }
  181. // KeyMarker is an option to set key-marker parameter
  182. func KeyMarker(value string) Option {
  183. return addParam("key-marker", value)
  184. }
  185. // UploadIDMarker is an option to set upload-id-marker parameter
  186. func UploadIDMarker(value string) Option {
  187. return addParam("upload-id-marker", value)
  188. }
  189. // MaxParts is an option to set max-parts parameter
  190. func MaxParts(value int) Option {
  191. return addParam("max-parts", strconv.Itoa(value))
  192. }
  193. // PartNumberMarker is an option to set part-number-marker parameter
  194. func PartNumberMarker(value int) Option {
  195. return addParam("part-number-marker", strconv.Itoa(value))
  196. }
  197. // DeleteObjectsQuiet false:DeleteObjects in verbose mode; true:DeleteObjects in quite mode. Default is false.
  198. func DeleteObjectsQuiet(isQuiet bool) Option {
  199. return addArg(deleteObjectsQuiet, isQuiet)
  200. }
  201. // StorageClass bucket storage class
  202. func StorageClass(value StorageClassType) Option {
  203. return addArg(storageClass, value)
  204. }
  205. // Checkpoint configuration
  206. type cpConfig struct {
  207. IsEnable bool
  208. FilePath string
  209. DirPath string
  210. }
  211. // Checkpoint sets the isEnable flag and checkpoint file path for DownloadFile/UploadFile.
  212. func Checkpoint(isEnable bool, filePath string) Option {
  213. return addArg(checkpointConfig, &cpConfig{IsEnable: isEnable, FilePath: filePath})
  214. }
  215. // CheckpointDir sets the isEnable flag and checkpoint dir path for DownloadFile/UploadFile.
  216. func CheckpointDir(isEnable bool, dirPath string) Option {
  217. return addArg(checkpointConfig, &cpConfig{IsEnable: isEnable, DirPath: dirPath})
  218. }
  219. // Routines DownloadFile/UploadFile routine count
  220. func Routines(n int) Option {
  221. return addArg(routineNum, n)
  222. }
  223. // InitCRC Init AppendObject CRC
  224. func InitCRC(initCRC uint64) Option {
  225. return addArg(initCRC64, initCRC)
  226. }
  227. // Progress set progress listener
  228. func Progress(listener ProgressListener) Option {
  229. return addArg(progressListener, listener)
  230. }
  231. // ResponseContentType is an option to set response-content-type param
  232. func ResponseContentType(value string) Option {
  233. return addParam("response-content-type", value)
  234. }
  235. // ResponseContentLanguage is an option to set response-content-language param
  236. func ResponseContentLanguage(value string) Option {
  237. return addParam("response-content-language", value)
  238. }
  239. // ResponseExpires is an option to set response-expires param
  240. func ResponseExpires(value string) Option {
  241. return addParam("response-expires", value)
  242. }
  243. // ResponseCacheControl is an option to set response-cache-control param
  244. func ResponseCacheControl(value string) Option {
  245. return addParam("response-cache-control", value)
  246. }
  247. // ResponseContentDisposition is an option to set response-content-disposition param
  248. func ResponseContentDisposition(value string) Option {
  249. return addParam("response-content-disposition", value)
  250. }
  251. // ResponseContentEncoding is an option to set response-content-encoding param
  252. func ResponseContentEncoding(value string) Option {
  253. return addParam("response-content-encoding", value)
  254. }
  255. // Process is an option to set x-oss-process param
  256. func Process(value string) Option {
  257. return addParam("x-oss-process", value)
  258. }
  259. func setHeader(key string, value interface{}) Option {
  260. return func(params map[string]optionValue) error {
  261. if value == nil {
  262. return nil
  263. }
  264. params[key] = optionValue{value, optionHTTP}
  265. return nil
  266. }
  267. }
  268. func addParam(key string, value interface{}) Option {
  269. return func(params map[string]optionValue) error {
  270. if value == nil {
  271. return nil
  272. }
  273. params[key] = optionValue{value, optionParam}
  274. return nil
  275. }
  276. }
  277. func addArg(key string, value interface{}) Option {
  278. return func(params map[string]optionValue) error {
  279. if value == nil {
  280. return nil
  281. }
  282. params[key] = optionValue{value, optionArg}
  283. return nil
  284. }
  285. }
  286. func handleOptions(headers map[string]string, options []Option) error {
  287. params := map[string]optionValue{}
  288. for _, option := range options {
  289. if option != nil {
  290. if err := option(params); err != nil {
  291. return err
  292. }
  293. }
  294. }
  295. for k, v := range params {
  296. if v.Type == optionHTTP {
  297. headers[k] = v.Value.(string)
  298. }
  299. }
  300. return nil
  301. }
  302. func getRawParams(options []Option) (map[string]interface{}, error) {
  303. // Option
  304. params := map[string]optionValue{}
  305. for _, option := range options {
  306. if option != nil {
  307. if err := option(params); err != nil {
  308. return nil, err
  309. }
  310. }
  311. }
  312. paramsm := map[string]interface{}{}
  313. // Serialize
  314. for k, v := range params {
  315. if v.Type == optionParam {
  316. vs := params[k]
  317. paramsm[k] = vs.Value.(string)
  318. }
  319. }
  320. return paramsm, nil
  321. }
  322. func findOption(options []Option, param string, defaultVal interface{}) (interface{}, error) {
  323. params := map[string]optionValue{}
  324. for _, option := range options {
  325. if option != nil {
  326. if err := option(params); err != nil {
  327. return nil, err
  328. }
  329. }
  330. }
  331. if val, ok := params[param]; ok {
  332. return val.Value, nil
  333. }
  334. return defaultVal, nil
  335. }
  336. func isOptionSet(options []Option, option string) (bool, interface{}, error) {
  337. params := map[string]optionValue{}
  338. for _, option := range options {
  339. if option != nil {
  340. if err := option(params); err != nil {
  341. return false, nil, err
  342. }
  343. }
  344. }
  345. if val, ok := params[option]; ok {
  346. return true, val.Value, nil
  347. }
  348. return false, nil, nil
  349. }