option.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. package oss
  2. import (
  3. "fmt"
  4. "net/http"
  5. "net/url"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. type optionType string
  11. const (
  12. optionParam optionType = "HTTPParameter" // URL parameter
  13. optionHTTP optionType = "HTTPHeader" // HTTP header
  14. optionArg optionType = "FuncArgument" // Function argument
  15. )
  16. const (
  17. deleteObjectsQuiet = "delete-objects-quiet"
  18. routineNum = "x-routine-num"
  19. checkpointConfig = "x-cp-config"
  20. initCRC64 = "init-crc64"
  21. progressListener = "x-progress-listener"
  22. storageClass = "storage-class"
  23. )
  24. type (
  25. optionValue struct {
  26. Value interface{}
  27. Type optionType
  28. }
  29. // Option HTTP option
  30. Option func(map[string]optionValue) error
  31. )
  32. // ACL is an option to set X-Oss-Acl header
  33. func ACL(acl ACLType) Option {
  34. return setHeader(HTTPHeaderOssACL, string(acl))
  35. }
  36. // ContentType is an option to set Content-Type header
  37. func ContentType(value string) Option {
  38. return setHeader(HTTPHeaderContentType, value)
  39. }
  40. // ContentLength is an option to set Content-Length header
  41. func ContentLength(length int64) Option {
  42. return setHeader(HTTPHeaderContentLength, strconv.FormatInt(length, 10))
  43. }
  44. // CacheControl is an option to set Cache-Control header
  45. func CacheControl(value string) Option {
  46. return setHeader(HTTPHeaderCacheControl, value)
  47. }
  48. // ContentDisposition is an option to set Content-Disposition header
  49. func ContentDisposition(value string) Option {
  50. return setHeader(HTTPHeaderContentDisposition, value)
  51. }
  52. // ContentEncoding is an option to set Content-Encoding header
  53. func ContentEncoding(value string) Option {
  54. return setHeader(HTTPHeaderContentEncoding, value)
  55. }
  56. // ContentLanguage is an option to set Content-Language header
  57. func ContentLanguage(value string) Option {
  58. return setHeader(HTTPHeaderContentLanguage, value)
  59. }
  60. // ContentMD5 is an option to set Content-MD5 header
  61. func ContentMD5(value string) Option {
  62. return setHeader(HTTPHeaderContentMD5, value)
  63. }
  64. // Expires is an option to set Expires header
  65. func Expires(t time.Time) Option {
  66. return setHeader(HTTPHeaderExpires, t.Format(http.TimeFormat))
  67. }
  68. // Meta is an option to set Meta header
  69. func Meta(key, value string) Option {
  70. return setHeader(HTTPHeaderOssMetaPrefix+key, value)
  71. }
  72. // Range is an option to set Range header, [start, end]
  73. func Range(start, end int64) Option {
  74. return setHeader(HTTPHeaderRange, fmt.Sprintf("bytes=%d-%d", start, end))
  75. }
  76. // NormalizedRange is an option to set Range header, such as 1024-2048 or 1024- or -2048
  77. func NormalizedRange(nr string) Option {
  78. return setHeader(HTTPHeaderRange, fmt.Sprintf("bytes=%s", strings.TrimSpace(nr)))
  79. }
  80. // AcceptEncoding is an option to set Accept-Encoding header
  81. func AcceptEncoding(value string) Option {
  82. return setHeader(HTTPHeaderAcceptEncoding, value)
  83. }
  84. // IfModifiedSince is an option to set If-Modified-Since header
  85. func IfModifiedSince(t time.Time) Option {
  86. return setHeader(HTTPHeaderIfModifiedSince, t.Format(http.TimeFormat))
  87. }
  88. // IfUnmodifiedSince is an option to set If-Unmodified-Since header
  89. func IfUnmodifiedSince(t time.Time) Option {
  90. return setHeader(HTTPHeaderIfUnmodifiedSince, t.Format(http.TimeFormat))
  91. }
  92. // IfMatch is an option to set If-Match header
  93. func IfMatch(value string) Option {
  94. return setHeader(HTTPHeaderIfMatch, value)
  95. }
  96. // IfNoneMatch is an option to set IfNoneMatch header
  97. func IfNoneMatch(value string) Option {
  98. return setHeader(HTTPHeaderIfNoneMatch, value)
  99. }
  100. // CopySource is an option to set X-Oss-Copy-Source header
  101. func CopySource(sourceBucket, sourceObject string) Option {
  102. return setHeader(HTTPHeaderOssCopySource, "/"+sourceBucket+"/"+sourceObject)
  103. }
  104. // CopySourceRange is an option to set X-Oss-Copy-Source header
  105. func CopySourceRange(startPosition, partSize int64) Option {
  106. val := "bytes=" + strconv.FormatInt(startPosition, 10) + "-" +
  107. strconv.FormatInt((startPosition+partSize-1), 10)
  108. return setHeader(HTTPHeaderOssCopySourceRange, val)
  109. }
  110. // CopySourceIfMatch is an option to set X-Oss-Copy-Source-If-Match header
  111. func CopySourceIfMatch(value string) Option {
  112. return setHeader(HTTPHeaderOssCopySourceIfMatch, value)
  113. }
  114. // CopySourceIfNoneMatch is an option to set X-Oss-Copy-Source-If-None-Match header
  115. func CopySourceIfNoneMatch(value string) Option {
  116. return setHeader(HTTPHeaderOssCopySourceIfNoneMatch, value)
  117. }
  118. // CopySourceIfModifiedSince is an option to set X-Oss-CopySource-If-Modified-Since header
  119. func CopySourceIfModifiedSince(t time.Time) Option {
  120. return setHeader(HTTPHeaderOssCopySourceIfModifiedSince, t.Format(http.TimeFormat))
  121. }
  122. // CopySourceIfUnmodifiedSince is an option to set X-Oss-Copy-Source-If-Unmodified-Since header
  123. func CopySourceIfUnmodifiedSince(t time.Time) Option {
  124. return setHeader(HTTPHeaderOssCopySourceIfUnmodifiedSince, t.Format(http.TimeFormat))
  125. }
  126. // MetadataDirective is an option to set X-Oss-Metadata-Directive header
  127. func MetadataDirective(directive MetadataDirectiveType) Option {
  128. return setHeader(HTTPHeaderOssMetadataDirective, string(directive))
  129. }
  130. // ServerSideEncryption is an option to set X-Oss-Server-Side-Encryption header
  131. func ServerSideEncryption(value string) Option {
  132. return setHeader(HTTPHeaderOssServerSideEncryption, value)
  133. }
  134. // ServerSideEncryptionKeyID is an option to set X-Oss-Server-Side-Encryption-Key-Id header
  135. func ServerSideEncryptionKeyID(value string) Option {
  136. return setHeader(HTTPHeaderOssServerSideEncryptionKeyID, value)
  137. }
  138. // ObjectACL is an option to set X-Oss-Object-Acl header
  139. func ObjectACL(acl ACLType) Option {
  140. return setHeader(HTTPHeaderOssObjectACL, string(acl))
  141. }
  142. // symlinkTarget is an option to set X-Oss-Symlink-Target
  143. func symlinkTarget(targetObjectKey string) Option {
  144. return setHeader(HTTPHeaderOssSymlinkTarget, targetObjectKey)
  145. }
  146. // Origin is an option to set Origin header
  147. func Origin(value string) Option {
  148. return setHeader(HTTPHeaderOrigin, value)
  149. }
  150. // ObjectStorageClass is an option to set the storage class of object
  151. func ObjectStorageClass(storageClass StorageClassType) Option {
  152. return setHeader(HTTPHeaderOssStorageClass, string(storageClass))
  153. }
  154. // Callback is an option to set callback values
  155. func Callback(callback string) Option {
  156. return setHeader(HTTPHeaderOssCallback, callback)
  157. }
  158. // CallbackVar is an option to set callback user defined values
  159. func CallbackVar(callbackVar string) Option {
  160. return setHeader(HTTPHeaderOssCallbackVar, callbackVar)
  161. }
  162. // RequestPayer is an option to set payer who pay for the request
  163. func RequestPayer(payerType PayerType) Option {
  164. return setHeader(HTTPHeaderOssRequester, string(payerType))
  165. }
  166. // SetTagging is an option to set object tagging
  167. func SetTagging(tagging Tagging) Option {
  168. if len(tagging.Tags) == 0 {
  169. return nil
  170. }
  171. taggingValue := ""
  172. for index, tag := range tagging.Tags {
  173. if index != 0 {
  174. taggingValue += "&"
  175. }
  176. taggingValue += url.QueryEscape(tag.Key) + "=" + url.QueryEscape(tag.Value)
  177. }
  178. return setHeader(HTTPHeaderOssTagging, taggingValue)
  179. }
  180. // TaggingDirective is an option to set X-Oss-Metadata-Directive header
  181. func TaggingDirective(directive TaggingDirectiveType) Option {
  182. return setHeader(HTTPHeaderOssTaggingDirective, string(directive))
  183. }
  184. // Delimiter is an option to set delimiler parameter
  185. func Delimiter(value string) Option {
  186. return addParam("delimiter", value)
  187. }
  188. // Marker is an option to set marker parameter
  189. func Marker(value string) Option {
  190. return addParam("marker", value)
  191. }
  192. // MaxKeys is an option to set maxkeys parameter
  193. func MaxKeys(value int) Option {
  194. return addParam("max-keys", strconv.Itoa(value))
  195. }
  196. // Prefix is an option to set prefix parameter
  197. func Prefix(value string) Option {
  198. return addParam("prefix", value)
  199. }
  200. // EncodingType is an option to set encoding-type parameter
  201. func EncodingType(value string) Option {
  202. return addParam("encoding-type", value)
  203. }
  204. // MaxUploads is an option to set max-uploads parameter
  205. func MaxUploads(value int) Option {
  206. return addParam("max-uploads", strconv.Itoa(value))
  207. }
  208. // KeyMarker is an option to set key-marker parameter
  209. func KeyMarker(value string) Option {
  210. return addParam("key-marker", value)
  211. }
  212. // UploadIDMarker is an option to set upload-id-marker parameter
  213. func UploadIDMarker(value string) Option {
  214. return addParam("upload-id-marker", value)
  215. }
  216. // MaxParts is an option to set max-parts parameter
  217. func MaxParts(value int) Option {
  218. return addParam("max-parts", strconv.Itoa(value))
  219. }
  220. // PartNumberMarker is an option to set part-number-marker parameter
  221. func PartNumberMarker(value int) Option {
  222. return addParam("part-number-marker", strconv.Itoa(value))
  223. }
  224. // DeleteObjectsQuiet false:DeleteObjects in verbose mode; true:DeleteObjects in quite mode. Default is false.
  225. func DeleteObjectsQuiet(isQuiet bool) Option {
  226. return addArg(deleteObjectsQuiet, isQuiet)
  227. }
  228. // StorageClass bucket storage class
  229. func StorageClass(value StorageClassType) Option {
  230. return addArg(storageClass, value)
  231. }
  232. // Checkpoint configuration
  233. type cpConfig struct {
  234. IsEnable bool
  235. FilePath string
  236. DirPath string
  237. }
  238. // Checkpoint sets the isEnable flag and checkpoint file path for DownloadFile/UploadFile.
  239. func Checkpoint(isEnable bool, filePath string) Option {
  240. return addArg(checkpointConfig, &cpConfig{IsEnable: isEnable, FilePath: filePath})
  241. }
  242. // CheckpointDir sets the isEnable flag and checkpoint dir path for DownloadFile/UploadFile.
  243. func CheckpointDir(isEnable bool, dirPath string) Option {
  244. return addArg(checkpointConfig, &cpConfig{IsEnable: isEnable, DirPath: dirPath})
  245. }
  246. // Routines DownloadFile/UploadFile routine count
  247. func Routines(n int) Option {
  248. return addArg(routineNum, n)
  249. }
  250. // InitCRC Init AppendObject CRC
  251. func InitCRC(initCRC uint64) Option {
  252. return addArg(initCRC64, initCRC)
  253. }
  254. // Progress set progress listener
  255. func Progress(listener ProgressListener) Option {
  256. return addArg(progressListener, listener)
  257. }
  258. // ResponseContentType is an option to set response-content-type param
  259. func ResponseContentType(value string) Option {
  260. return addParam("response-content-type", value)
  261. }
  262. // ResponseContentLanguage is an option to set response-content-language param
  263. func ResponseContentLanguage(value string) Option {
  264. return addParam("response-content-language", value)
  265. }
  266. // ResponseExpires is an option to set response-expires param
  267. func ResponseExpires(value string) Option {
  268. return addParam("response-expires", value)
  269. }
  270. // ResponseCacheControl is an option to set response-cache-control param
  271. func ResponseCacheControl(value string) Option {
  272. return addParam("response-cache-control", value)
  273. }
  274. // ResponseContentDisposition is an option to set response-content-disposition param
  275. func ResponseContentDisposition(value string) Option {
  276. return addParam("response-content-disposition", value)
  277. }
  278. // ResponseContentEncoding is an option to set response-content-encoding param
  279. func ResponseContentEncoding(value string) Option {
  280. return addParam("response-content-encoding", value)
  281. }
  282. // Process is an option to set x-oss-process param
  283. func Process(value string) Option {
  284. return addParam("x-oss-process", value)
  285. }
  286. func setHeader(key string, value interface{}) Option {
  287. return func(params map[string]optionValue) error {
  288. if value == nil {
  289. return nil
  290. }
  291. params[key] = optionValue{value, optionHTTP}
  292. return nil
  293. }
  294. }
  295. func addParam(key string, value interface{}) Option {
  296. return func(params map[string]optionValue) error {
  297. if value == nil {
  298. return nil
  299. }
  300. params[key] = optionValue{value, optionParam}
  301. return nil
  302. }
  303. }
  304. func addArg(key string, value interface{}) Option {
  305. return func(params map[string]optionValue) error {
  306. if value == nil {
  307. return nil
  308. }
  309. params[key] = optionValue{value, optionArg}
  310. return nil
  311. }
  312. }
  313. func handleOptions(headers map[string]string, options []Option) error {
  314. params := map[string]optionValue{}
  315. for _, option := range options {
  316. if option != nil {
  317. if err := option(params); err != nil {
  318. return err
  319. }
  320. }
  321. }
  322. for k, v := range params {
  323. if v.Type == optionHTTP {
  324. headers[k] = v.Value.(string)
  325. }
  326. }
  327. return nil
  328. }
  329. func getRawParams(options []Option) (map[string]interface{}, error) {
  330. // Option
  331. params := map[string]optionValue{}
  332. for _, option := range options {
  333. if option != nil {
  334. if err := option(params); err != nil {
  335. return nil, err
  336. }
  337. }
  338. }
  339. paramsm := map[string]interface{}{}
  340. // Serialize
  341. for k, v := range params {
  342. if v.Type == optionParam {
  343. vs := params[k]
  344. paramsm[k] = vs.Value.(string)
  345. }
  346. }
  347. return paramsm, nil
  348. }
  349. func findOption(options []Option, param string, defaultVal interface{}) (interface{}, error) {
  350. params := map[string]optionValue{}
  351. for _, option := range options {
  352. if option != nil {
  353. if err := option(params); err != nil {
  354. return nil, err
  355. }
  356. }
  357. }
  358. if val, ok := params[param]; ok {
  359. return val.Value, nil
  360. }
  361. return defaultVal, nil
  362. }
  363. func isOptionSet(options []Option, option string) (bool, interface{}, error) {
  364. params := map[string]optionValue{}
  365. for _, option := range options {
  366. if option != nil {
  367. if err := option(params); err != nil {
  368. return false, nil, err
  369. }
  370. }
  371. }
  372. if val, ok := params[option]; ok {
  373. return true, val.Value, nil
  374. }
  375. return false, nil, nil
  376. }