option.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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. responseHeader = "x-response-header"
  24. redundancyType = "redundancy-type"
  25. )
  26. type (
  27. optionValue struct {
  28. Value interface{}
  29. Type optionType
  30. }
  31. // Option HTTP option
  32. Option func(map[string]optionValue) error
  33. )
  34. // ACL is an option to set X-Oss-Acl header
  35. func ACL(acl ACLType) Option {
  36. return setHeader(HTTPHeaderOssACL, string(acl))
  37. }
  38. // ContentType is an option to set Content-Type header
  39. func ContentType(value string) Option {
  40. return setHeader(HTTPHeaderContentType, value)
  41. }
  42. // ContentLength is an option to set Content-Length header
  43. func ContentLength(length int64) Option {
  44. return setHeader(HTTPHeaderContentLength, strconv.FormatInt(length, 10))
  45. }
  46. // CacheControl is an option to set Cache-Control header
  47. func CacheControl(value string) Option {
  48. return setHeader(HTTPHeaderCacheControl, value)
  49. }
  50. // ContentDisposition is an option to set Content-Disposition header
  51. func ContentDisposition(value string) Option {
  52. return setHeader(HTTPHeaderContentDisposition, value)
  53. }
  54. // ContentEncoding is an option to set Content-Encoding header
  55. func ContentEncoding(value string) Option {
  56. return setHeader(HTTPHeaderContentEncoding, value)
  57. }
  58. // ContentLanguage is an option to set Content-Language header
  59. func ContentLanguage(value string) Option {
  60. return setHeader(HTTPHeaderContentLanguage, value)
  61. }
  62. // ContentMD5 is an option to set Content-MD5 header
  63. func ContentMD5(value string) Option {
  64. return setHeader(HTTPHeaderContentMD5, value)
  65. }
  66. // Expires is an option to set Expires header
  67. func Expires(t time.Time) Option {
  68. return setHeader(HTTPHeaderExpires, t.Format(http.TimeFormat))
  69. }
  70. // Meta is an option to set Meta header
  71. func Meta(key, value string) Option {
  72. return setHeader(HTTPHeaderOssMetaPrefix+key, value)
  73. }
  74. // Range is an option to set Range header, [start, end]
  75. func Range(start, end int64) Option {
  76. return setHeader(HTTPHeaderRange, fmt.Sprintf("bytes=%d-%d", start, end))
  77. }
  78. // NormalizedRange is an option to set Range header, such as 1024-2048 or 1024- or -2048
  79. func NormalizedRange(nr string) Option {
  80. return setHeader(HTTPHeaderRange, fmt.Sprintf("bytes=%s", strings.TrimSpace(nr)))
  81. }
  82. // AcceptEncoding is an option to set Accept-Encoding header
  83. func AcceptEncoding(value string) Option {
  84. return setHeader(HTTPHeaderAcceptEncoding, value)
  85. }
  86. // IfModifiedSince is an option to set If-Modified-Since header
  87. func IfModifiedSince(t time.Time) Option {
  88. return setHeader(HTTPHeaderIfModifiedSince, t.Format(http.TimeFormat))
  89. }
  90. // IfUnmodifiedSince is an option to set If-Unmodified-Since header
  91. func IfUnmodifiedSince(t time.Time) Option {
  92. return setHeader(HTTPHeaderIfUnmodifiedSince, t.Format(http.TimeFormat))
  93. }
  94. // IfMatch is an option to set If-Match header
  95. func IfMatch(value string) Option {
  96. return setHeader(HTTPHeaderIfMatch, value)
  97. }
  98. // IfNoneMatch is an option to set IfNoneMatch header
  99. func IfNoneMatch(value string) Option {
  100. return setHeader(HTTPHeaderIfNoneMatch, value)
  101. }
  102. // CopySource is an option to set X-Oss-Copy-Source header
  103. func CopySource(sourceBucket, sourceObject string) Option {
  104. return setHeader(HTTPHeaderOssCopySource, "/"+sourceBucket+"/"+sourceObject)
  105. }
  106. // CopySourceVersion is an option to set X-Oss-Copy-Source header,include versionId
  107. func CopySourceVersion(sourceBucket, sourceObject string, versionId string) Option {
  108. return setHeader(HTTPHeaderOssCopySource, "/"+sourceBucket+"/"+sourceObject+"?"+"versionId="+versionId)
  109. }
  110. // CopySourceRange is an option to set X-Oss-Copy-Source header
  111. func CopySourceRange(startPosition, partSize int64) Option {
  112. val := "bytes=" + strconv.FormatInt(startPosition, 10) + "-" +
  113. strconv.FormatInt((startPosition+partSize-1), 10)
  114. return setHeader(HTTPHeaderOssCopySourceRange, val)
  115. }
  116. // CopySourceIfMatch is an option to set X-Oss-Copy-Source-If-Match header
  117. func CopySourceIfMatch(value string) Option {
  118. return setHeader(HTTPHeaderOssCopySourceIfMatch, value)
  119. }
  120. // CopySourceIfNoneMatch is an option to set X-Oss-Copy-Source-If-None-Match header
  121. func CopySourceIfNoneMatch(value string) Option {
  122. return setHeader(HTTPHeaderOssCopySourceIfNoneMatch, value)
  123. }
  124. // CopySourceIfModifiedSince is an option to set X-Oss-CopySource-If-Modified-Since header
  125. func CopySourceIfModifiedSince(t time.Time) Option {
  126. return setHeader(HTTPHeaderOssCopySourceIfModifiedSince, t.Format(http.TimeFormat))
  127. }
  128. // CopySourceIfUnmodifiedSince is an option to set X-Oss-Copy-Source-If-Unmodified-Since header
  129. func CopySourceIfUnmodifiedSince(t time.Time) Option {
  130. return setHeader(HTTPHeaderOssCopySourceIfUnmodifiedSince, t.Format(http.TimeFormat))
  131. }
  132. // MetadataDirective is an option to set X-Oss-Metadata-Directive header
  133. func MetadataDirective(directive MetadataDirectiveType) Option {
  134. return setHeader(HTTPHeaderOssMetadataDirective, string(directive))
  135. }
  136. // ServerSideEncryption is an option to set X-Oss-Server-Side-Encryption header
  137. func ServerSideEncryption(value string) Option {
  138. return setHeader(HTTPHeaderOssServerSideEncryption, value)
  139. }
  140. // ServerSideEncryptionKeyID is an option to set X-Oss-Server-Side-Encryption-Key-Id header
  141. func ServerSideEncryptionKeyID(value string) Option {
  142. return setHeader(HTTPHeaderOssServerSideEncryptionKeyID, value)
  143. }
  144. // ServerSideDataEncryption is an option to set X-Oss-Server-Side-Data-Encryption header
  145. func ServerSideDataEncryption(value string) Option {
  146. return setHeader(HTTPHeaderOssServerSideDataEncryption, value)
  147. }
  148. // SSECAlgorithm is an option to set X-Oss-Server-Side-Encryption-Customer-Algorithm header
  149. func SSECAlgorithm(value string) Option {
  150. return setHeader(HTTPHeaderSSECAlgorithm, value)
  151. }
  152. // SSECKey is an option to set X-Oss-Server-Side-Encryption-Customer-Key header
  153. func SSECKey(value string) Option {
  154. return setHeader(HTTPHeaderSSECKey, value)
  155. }
  156. // SSECKeyMd5 is an option to set X-Oss-Server-Side-Encryption-Customer-Key-Md5 header
  157. func SSECKeyMd5(value string) Option {
  158. return setHeader(HTTPHeaderSSECKeyMd5, value)
  159. }
  160. // ObjectACL is an option to set X-Oss-Object-Acl header
  161. func ObjectACL(acl ACLType) Option {
  162. return setHeader(HTTPHeaderOssObjectACL, string(acl))
  163. }
  164. // symlinkTarget is an option to set X-Oss-Symlink-Target
  165. func symlinkTarget(targetObjectKey string) Option {
  166. return setHeader(HTTPHeaderOssSymlinkTarget, targetObjectKey)
  167. }
  168. // Origin is an option to set Origin header
  169. func Origin(value string) Option {
  170. return setHeader(HTTPHeaderOrigin, value)
  171. }
  172. // ObjectStorageClass is an option to set the storage class of object
  173. func ObjectStorageClass(storageClass StorageClassType) Option {
  174. return setHeader(HTTPHeaderOssStorageClass, string(storageClass))
  175. }
  176. // Callback is an option to set callback values
  177. func Callback(callback string) Option {
  178. return setHeader(HTTPHeaderOssCallback, callback)
  179. }
  180. // CallbackVar is an option to set callback user defined values
  181. func CallbackVar(callbackVar string) Option {
  182. return setHeader(HTTPHeaderOssCallbackVar, callbackVar)
  183. }
  184. // RequestPayer is an option to set payer who pay for the request
  185. func RequestPayer(payerType PayerType) Option {
  186. return setHeader(HTTPHeaderOssRequester, strings.ToLower(string(payerType)))
  187. }
  188. // RequestPayerParam is an option to set payer who pay for the request
  189. func RequestPayerParam(payerType PayerType) Option {
  190. return addParam(strings.ToLower(HTTPHeaderOssRequester), strings.ToLower(string(payerType)))
  191. }
  192. // SetTagging is an option to set object tagging
  193. func SetTagging(tagging Tagging) Option {
  194. if len(tagging.Tags) == 0 {
  195. return nil
  196. }
  197. taggingValue := ""
  198. for index, tag := range tagging.Tags {
  199. if index != 0 {
  200. taggingValue += "&"
  201. }
  202. taggingValue += url.QueryEscape(tag.Key) + "=" + url.QueryEscape(tag.Value)
  203. }
  204. return setHeader(HTTPHeaderOssTagging, taggingValue)
  205. }
  206. // TaggingDirective is an option to set X-Oss-Metadata-Directive header
  207. func TaggingDirective(directive TaggingDirectiveType) Option {
  208. return setHeader(HTTPHeaderOssTaggingDirective, string(directive))
  209. }
  210. // ACReqMethod is an option to set Access-Control-Request-Method header
  211. func ACReqMethod(value string) Option {
  212. return setHeader(HTTPHeaderACReqMethod, value)
  213. }
  214. // ACReqHeaders is an option to set Access-Control-Request-Headers header
  215. func ACReqHeaders(value string) Option {
  216. return setHeader(HTTPHeaderACReqHeaders, value)
  217. }
  218. // TrafficLimitHeader is an option to set X-Oss-Traffic-Limit
  219. func TrafficLimitHeader(value int64) Option {
  220. return setHeader(HTTPHeaderOssTrafficLimit, strconv.FormatInt(value, 10))
  221. }
  222. // UserAgentHeader is an option to set HTTPHeaderUserAgent
  223. func UserAgentHeader(ua string) Option {
  224. return setHeader(HTTPHeaderUserAgent, ua)
  225. }
  226. // ForbidOverWrite is an option to set X-Oss-Forbid-Overwrite
  227. func ForbidOverWrite(forbidWrite bool) Option {
  228. if forbidWrite {
  229. return setHeader(HTTPHeaderOssForbidOverWrite, "true")
  230. } else {
  231. return setHeader(HTTPHeaderOssForbidOverWrite, "false")
  232. }
  233. }
  234. // RangeBehavior is an option to set Range value, such as "standard"
  235. func RangeBehavior(value string) Option {
  236. return setHeader(HTTPHeaderOssRangeBehavior, value)
  237. }
  238. // Delimiter is an option to set delimiler parameter
  239. func Delimiter(value string) Option {
  240. return addParam("delimiter", value)
  241. }
  242. // Marker is an option to set marker parameter
  243. func Marker(value string) Option {
  244. return addParam("marker", value)
  245. }
  246. // MaxKeys is an option to set maxkeys parameter
  247. func MaxKeys(value int) Option {
  248. return addParam("max-keys", strconv.Itoa(value))
  249. }
  250. // Prefix is an option to set prefix parameter
  251. func Prefix(value string) Option {
  252. return addParam("prefix", value)
  253. }
  254. // EncodingType is an option to set encoding-type parameter
  255. func EncodingType(value string) Option {
  256. return addParam("encoding-type", value)
  257. }
  258. // MaxUploads is an option to set max-uploads parameter
  259. func MaxUploads(value int) Option {
  260. return addParam("max-uploads", strconv.Itoa(value))
  261. }
  262. // KeyMarker is an option to set key-marker parameter
  263. func KeyMarker(value string) Option {
  264. return addParam("key-marker", value)
  265. }
  266. // VersionIdMarker is an option to set version-id-marker parameter
  267. func VersionIdMarker(value string) Option {
  268. return addParam("version-id-marker", value)
  269. }
  270. // VersionId is an option to set versionId parameter
  271. func VersionId(value string) Option {
  272. return addParam("versionId", value)
  273. }
  274. // TagKey is an option to set tag key parameter
  275. func TagKey(value string) Option {
  276. return addParam("tag-key", value)
  277. }
  278. // TagValue is an option to set tag value parameter
  279. func TagValue(value string) Option {
  280. return addParam("tag-value", value)
  281. }
  282. // UploadIDMarker is an option to set upload-id-marker parameter
  283. func UploadIDMarker(value string) Option {
  284. return addParam("upload-id-marker", value)
  285. }
  286. // MaxParts is an option to set max-parts parameter
  287. func MaxParts(value int) Option {
  288. return addParam("max-parts", strconv.Itoa(value))
  289. }
  290. // PartNumberMarker is an option to set part-number-marker parameter
  291. func PartNumberMarker(value int) Option {
  292. return addParam("part-number-marker", strconv.Itoa(value))
  293. }
  294. // Sequential is an option to set sequential parameter for InitiateMultipartUpload
  295. func Sequential() Option {
  296. return addParam("sequential", "")
  297. }
  298. // DeleteObjectsQuiet false:DeleteObjects in verbose mode; true:DeleteObjects in quite mode. Default is false.
  299. func DeleteObjectsQuiet(isQuiet bool) Option {
  300. return addArg(deleteObjectsQuiet, isQuiet)
  301. }
  302. // StorageClass bucket storage class
  303. func StorageClass(value StorageClassType) Option {
  304. return addArg(storageClass, value)
  305. }
  306. // RedundancyType bucket data redundancy type
  307. func RedundancyType(value DataRedundancyType) Option {
  308. return addArg(redundancyType, value)
  309. }
  310. // Checkpoint configuration
  311. type cpConfig struct {
  312. IsEnable bool
  313. FilePath string
  314. DirPath string
  315. }
  316. // Checkpoint sets the isEnable flag and checkpoint file path for DownloadFile/UploadFile.
  317. func Checkpoint(isEnable bool, filePath string) Option {
  318. return addArg(checkpointConfig, &cpConfig{IsEnable: isEnable, FilePath: filePath})
  319. }
  320. // CheckpointDir sets the isEnable flag and checkpoint dir path for DownloadFile/UploadFile.
  321. func CheckpointDir(isEnable bool, dirPath string) Option {
  322. return addArg(checkpointConfig, &cpConfig{IsEnable: isEnable, DirPath: dirPath})
  323. }
  324. // Routines DownloadFile/UploadFile routine count
  325. func Routines(n int) Option {
  326. return addArg(routineNum, n)
  327. }
  328. // InitCRC Init AppendObject CRC
  329. func InitCRC(initCRC uint64) Option {
  330. return addArg(initCRC64, initCRC)
  331. }
  332. // Progress set progress listener
  333. func Progress(listener ProgressListener) Option {
  334. return addArg(progressListener, listener)
  335. }
  336. // GetResponseHeader for get response http header
  337. func GetResponseHeader(respHeader *http.Header) Option {
  338. return addArg(responseHeader, respHeader)
  339. }
  340. // ResponseContentType is an option to set response-content-type param
  341. func ResponseContentType(value string) Option {
  342. return addParam("response-content-type", value)
  343. }
  344. // ResponseContentLanguage is an option to set response-content-language param
  345. func ResponseContentLanguage(value string) Option {
  346. return addParam("response-content-language", value)
  347. }
  348. // ResponseExpires is an option to set response-expires param
  349. func ResponseExpires(value string) Option {
  350. return addParam("response-expires", value)
  351. }
  352. // ResponseCacheControl is an option to set response-cache-control param
  353. func ResponseCacheControl(value string) Option {
  354. return addParam("response-cache-control", value)
  355. }
  356. // ResponseContentDisposition is an option to set response-content-disposition param
  357. func ResponseContentDisposition(value string) Option {
  358. return addParam("response-content-disposition", value)
  359. }
  360. // ResponseContentEncoding is an option to set response-content-encoding param
  361. func ResponseContentEncoding(value string) Option {
  362. return addParam("response-content-encoding", value)
  363. }
  364. // Process is an option to set x-oss-process param
  365. func Process(value string) Option {
  366. return addParam("x-oss-process", value)
  367. }
  368. // TrafficLimitParam is a option to set x-oss-traffic-limit
  369. func TrafficLimitParam(value int64) Option {
  370. return addParam("x-oss-traffic-limit", strconv.FormatInt(value, 10))
  371. }
  372. // SetHeader Allow users to set personalized http headers
  373. func SetHeader(key string, value interface{}) Option {
  374. return setHeader(key, value)
  375. }
  376. // AddParam Allow users to set personalized http params
  377. func AddParam(key string, value interface{}) Option {
  378. return addParam(key, value)
  379. }
  380. func setHeader(key string, value interface{}) Option {
  381. return func(params map[string]optionValue) error {
  382. if value == nil {
  383. return nil
  384. }
  385. params[key] = optionValue{value, optionHTTP}
  386. return nil
  387. }
  388. }
  389. func addParam(key string, value interface{}) Option {
  390. return func(params map[string]optionValue) error {
  391. if value == nil {
  392. return nil
  393. }
  394. params[key] = optionValue{value, optionParam}
  395. return nil
  396. }
  397. }
  398. func addArg(key string, value interface{}) Option {
  399. return func(params map[string]optionValue) error {
  400. if value == nil {
  401. return nil
  402. }
  403. params[key] = optionValue{value, optionArg}
  404. return nil
  405. }
  406. }
  407. func handleOptions(headers map[string]string, options []Option) error {
  408. params := map[string]optionValue{}
  409. for _, option := range options {
  410. if option != nil {
  411. if err := option(params); err != nil {
  412. return err
  413. }
  414. }
  415. }
  416. for k, v := range params {
  417. if v.Type == optionHTTP {
  418. headers[k] = v.Value.(string)
  419. }
  420. }
  421. return nil
  422. }
  423. func GetRawParams(options []Option) (map[string]interface{}, error) {
  424. // Option
  425. params := map[string]optionValue{}
  426. for _, option := range options {
  427. if option != nil {
  428. if err := option(params); err != nil {
  429. return nil, err
  430. }
  431. }
  432. }
  433. paramsm := map[string]interface{}{}
  434. // Serialize
  435. for k, v := range params {
  436. if v.Type == optionParam {
  437. vs := params[k]
  438. paramsm[k] = vs.Value.(string)
  439. }
  440. }
  441. return paramsm, nil
  442. }
  443. func FindOption(options []Option, param string, defaultVal interface{}) (interface{}, error) {
  444. params := map[string]optionValue{}
  445. for _, option := range options {
  446. if option != nil {
  447. if err := option(params); err != nil {
  448. return nil, err
  449. }
  450. }
  451. }
  452. if val, ok := params[param]; ok {
  453. return val.Value, nil
  454. }
  455. return defaultVal, nil
  456. }
  457. func IsOptionSet(options []Option, option string) (bool, interface{}, error) {
  458. params := map[string]optionValue{}
  459. for _, option := range options {
  460. if option != nil {
  461. if err := option(params); err != nil {
  462. return false, nil, err
  463. }
  464. }
  465. }
  466. if val, ok := params[option]; ok {
  467. return true, val.Value, nil
  468. }
  469. return false, nil, nil
  470. }
  471. func DeleteOption(options []Option, strKey string) []Option {
  472. var outOption []Option
  473. params := map[string]optionValue{}
  474. for _, option := range options {
  475. if option != nil {
  476. option(params)
  477. _, exist := params[strKey]
  478. if !exist {
  479. outOption = append(outOption, option)
  480. } else {
  481. delete(params, strKey)
  482. }
  483. }
  484. }
  485. return outOption
  486. }
  487. func GetRequestId(header http.Header) string {
  488. return header.Get("x-oss-request-id")
  489. }
  490. func GetVersionId(header http.Header) string {
  491. return header.Get("x-oss-version-id")
  492. }
  493. func GetCopySrcVersionId(header http.Header) string {
  494. return header.Get("x-oss-copy-source-version-id")
  495. }
  496. func GetDeleteMark(header http.Header) bool {
  497. value := header.Get("x-oss-delete-marker")
  498. if strings.ToUpper(value) == "TRUE" {
  499. return true
  500. }
  501. return false
  502. }
  503. func GetQosDelayTime(header http.Header) string {
  504. return header.Get("x-oss-qos-delay-time")
  505. }