client.go 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  1. // Package oss implements functions for access oss service.
  2. // It has two main struct Client and Bucket.
  3. package oss
  4. import (
  5. "bytes"
  6. "encoding/xml"
  7. "fmt"
  8. "io"
  9. "io/ioutil"
  10. "log"
  11. "net"
  12. "net/http"
  13. "strings"
  14. "time"
  15. )
  16. // Client SDK's entry point. It's for bucket related options such as create/delete/set bucket (such as set/get ACL/lifecycle/referer/logging/website).
  17. // Object related operations are done by Bucket class.
  18. // Users use oss.New to create Client instance.
  19. //
  20. type (
  21. // Client OSS client
  22. Client struct {
  23. Config *Config // OSS client configuration
  24. Conn *Conn // Send HTTP request
  25. HTTPClient *http.Client //http.Client to use - if nil will make its own
  26. }
  27. // ClientOption client option such as UseCname, Timeout, SecurityToken.
  28. ClientOption func(*Client)
  29. )
  30. // New creates a new client.
  31. //
  32. // endpoint the OSS datacenter endpoint such as http://oss-cn-hangzhou.aliyuncs.com .
  33. // accessKeyId access key Id.
  34. // accessKeySecret access key secret.
  35. //
  36. // Client creates the new client instance, the returned value is valid when error is nil.
  37. // error it's nil if no error, otherwise it's an error object.
  38. //
  39. func New(endpoint, accessKeyID, accessKeySecret string, options ...ClientOption) (*Client, error) {
  40. // Configuration
  41. config := getDefaultOssConfig()
  42. config.Endpoint = endpoint
  43. config.AccessKeyID = accessKeyID
  44. config.AccessKeySecret = accessKeySecret
  45. // URL parse
  46. url := &urlMaker{}
  47. url.Init(config.Endpoint, config.IsCname, config.IsUseProxy)
  48. // HTTP connect
  49. conn := &Conn{config: config, url: url}
  50. // OSS client
  51. client := &Client{
  52. Config: config,
  53. Conn: conn,
  54. }
  55. // Client options parse
  56. for _, option := range options {
  57. option(client)
  58. }
  59. // Create HTTP connection
  60. err := conn.init(config, url, client.HTTPClient)
  61. return client, err
  62. }
  63. // Bucket gets the bucket instance.
  64. //
  65. // bucketName the bucket name.
  66. // Bucket the bucket object, when error is nil.
  67. //
  68. // error it's nil if no error, otherwise it's an error object.
  69. //
  70. func (client Client) Bucket(bucketName string) (*Bucket, error) {
  71. return &Bucket{
  72. client,
  73. bucketName,
  74. }, nil
  75. }
  76. // CreateBucket creates a bucket.
  77. //
  78. // bucketName the bucket name, it's globably unique and immutable. The bucket name can only consist of lowercase letters, numbers and dash ('-').
  79. // It must start with lowercase letter or number and the length can only be between 3 and 255.
  80. // options options for creating the bucket, with optional ACL. The ACL could be ACLPrivate, ACLPublicRead, and ACLPublicReadWrite. By default it's ACLPrivate.
  81. // It could also be specified with StorageClass option, which supports StorageStandard, StorageIA(infrequent access), StorageArchive.
  82. //
  83. // error it's nil if no error, otherwise it's an error object.
  84. //
  85. func (client Client) CreateBucket(bucketName string, options ...Option) error {
  86. headers := make(map[string]string)
  87. handleOptions(headers, options)
  88. buffer := new(bytes.Buffer)
  89. isOptSet, val, _ := IsOptionSet(options, storageClass)
  90. if isOptSet {
  91. cbConfig := createBucketConfiguration{StorageClass: val.(StorageClassType)}
  92. bs, err := xml.Marshal(cbConfig)
  93. if err != nil {
  94. return err
  95. }
  96. buffer.Write(bs)
  97. contentType := http.DetectContentType(buffer.Bytes())
  98. headers[HTTPHeaderContentType] = contentType
  99. }
  100. params := map[string]interface{}{}
  101. resp, err := client.do("PUT", bucketName, params, headers, buffer, options...)
  102. if err != nil {
  103. return err
  104. }
  105. defer resp.Body.Close()
  106. return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
  107. }
  108. // ListBuckets lists buckets of the current account under the given endpoint, with optional filters.
  109. //
  110. // options specifies the filters such as Prefix, Marker and MaxKeys. Prefix is the bucket name's prefix filter.
  111. // And marker makes sure the returned buckets' name are greater than it in lexicographic order.
  112. // Maxkeys limits the max keys to return, and by default it's 100 and up to 1000.
  113. // For the common usage scenario, please check out list_bucket.go in the sample.
  114. // ListBucketsResponse the response object if error is nil.
  115. //
  116. // error it's nil if no error, otherwise it's an error object.
  117. //
  118. func (client Client) ListBuckets(options ...Option) (ListBucketsResult, error) {
  119. var out ListBucketsResult
  120. params, err := GetRawParams(options)
  121. if err != nil {
  122. return out, err
  123. }
  124. resp, err := client.do("GET", "", params, nil, nil, options...)
  125. if err != nil {
  126. return out, err
  127. }
  128. defer resp.Body.Close()
  129. err = xmlUnmarshal(resp.Body, &out)
  130. return out, err
  131. }
  132. // IsBucketExist checks if the bucket exists
  133. //
  134. // bucketName the bucket name.
  135. //
  136. // bool true if it exists, and it's only valid when error is nil.
  137. // error it's nil if no error, otherwise it's an error object.
  138. //
  139. func (client Client) IsBucketExist(bucketName string) (bool, error) {
  140. listRes, err := client.ListBuckets(Prefix(bucketName), MaxKeys(1))
  141. if err != nil {
  142. return false, err
  143. }
  144. if len(listRes.Buckets) == 1 && listRes.Buckets[0].Name == bucketName {
  145. return true, nil
  146. }
  147. return false, nil
  148. }
  149. // DeleteBucket deletes the bucket. Only empty bucket can be deleted (no object and parts).
  150. //
  151. // bucketName the bucket name.
  152. //
  153. // error it's nil if no error, otherwise it's an error object.
  154. //
  155. func (client Client) DeleteBucket(bucketName string, options ...Option) error {
  156. params := map[string]interface{}{}
  157. resp, err := client.do("DELETE", bucketName, params, nil, nil, options...)
  158. if err != nil {
  159. return err
  160. }
  161. defer resp.Body.Close()
  162. return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent})
  163. }
  164. // GetBucketLocation gets the bucket location.
  165. //
  166. // Checks out the following link for more information :
  167. // https://help.aliyun.com/document_detail/oss/user_guide/oss_concept/endpoint.html
  168. //
  169. // bucketName the bucket name
  170. //
  171. // string bucket's datacenter location
  172. // error it's nil if no error, otherwise it's an error object.
  173. //
  174. func (client Client) GetBucketLocation(bucketName string) (string, error) {
  175. params := map[string]interface{}{}
  176. params["location"] = nil
  177. resp, err := client.do("GET", bucketName, params, nil, nil)
  178. if err != nil {
  179. return "", err
  180. }
  181. defer resp.Body.Close()
  182. var LocationConstraint string
  183. err = xmlUnmarshal(resp.Body, &LocationConstraint)
  184. return LocationConstraint, err
  185. }
  186. // SetBucketACL sets bucket's ACL.
  187. //
  188. // bucketName the bucket name
  189. // bucketAcl the bucket ACL: ACLPrivate, ACLPublicRead and ACLPublicReadWrite.
  190. //
  191. // error it's nil if no error, otherwise it's an error object.
  192. //
  193. func (client Client) SetBucketACL(bucketName string, bucketACL ACLType) error {
  194. headers := map[string]string{HTTPHeaderOssACL: string(bucketACL)}
  195. params := map[string]interface{}{}
  196. params["acl"] = nil
  197. resp, err := client.do("PUT", bucketName, params, headers, nil)
  198. if err != nil {
  199. return err
  200. }
  201. defer resp.Body.Close()
  202. return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
  203. }
  204. // GetBucketACL gets the bucket ACL.
  205. //
  206. // bucketName the bucket name.
  207. //
  208. // GetBucketAclResponse the result object, and it's only valid when error is nil.
  209. // error it's nil if no error, otherwise it's an error object.
  210. //
  211. func (client Client) GetBucketACL(bucketName string) (GetBucketACLResult, error) {
  212. var out GetBucketACLResult
  213. params := map[string]interface{}{}
  214. params["acl"] = nil
  215. resp, err := client.do("GET", bucketName, params, nil, nil)
  216. if err != nil {
  217. return out, err
  218. }
  219. defer resp.Body.Close()
  220. err = xmlUnmarshal(resp.Body, &out)
  221. return out, err
  222. }
  223. // SetBucketLifecycle sets the bucket's lifecycle.
  224. //
  225. // For more information, checks out following link:
  226. // https://help.aliyun.com/document_detail/oss/user_guide/manage_object/object_lifecycle.html
  227. //
  228. // bucketName the bucket name.
  229. // rules the lifecycle rules. There're two kind of rules: absolute time expiration and relative time expiration in days and day/month/year respectively.
  230. // Check out sample/bucket_lifecycle.go for more details.
  231. //
  232. // error it's nil if no error, otherwise it's an error object.
  233. //
  234. func (client Client) SetBucketLifecycle(bucketName string, rules []LifecycleRule) error {
  235. err := verifyLifecycleRules(rules)
  236. if err != nil {
  237. return err
  238. }
  239. lifecycleCfg := LifecycleConfiguration{Rules: rules}
  240. bs, err := xml.Marshal(lifecycleCfg)
  241. if err != nil {
  242. return err
  243. }
  244. buffer := new(bytes.Buffer)
  245. buffer.Write(bs)
  246. contentType := http.DetectContentType(buffer.Bytes())
  247. headers := map[string]string{}
  248. headers[HTTPHeaderContentType] = contentType
  249. params := map[string]interface{}{}
  250. params["lifecycle"] = nil
  251. resp, err := client.do("PUT", bucketName, params, headers, buffer)
  252. if err != nil {
  253. return err
  254. }
  255. defer resp.Body.Close()
  256. return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
  257. }
  258. // DeleteBucketLifecycle deletes the bucket's lifecycle.
  259. //
  260. //
  261. // bucketName the bucket name.
  262. //
  263. // error it's nil if no error, otherwise it's an error object.
  264. //
  265. func (client Client) DeleteBucketLifecycle(bucketName string) error {
  266. params := map[string]interface{}{}
  267. params["lifecycle"] = nil
  268. resp, err := client.do("DELETE", bucketName, params, nil, nil)
  269. if err != nil {
  270. return err
  271. }
  272. defer resp.Body.Close()
  273. return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent})
  274. }
  275. // GetBucketLifecycle gets the bucket's lifecycle settings.
  276. //
  277. // bucketName the bucket name.
  278. //
  279. // GetBucketLifecycleResponse the result object upon successful request. It's only valid when error is nil.
  280. // error it's nil if no error, otherwise it's an error object.
  281. //
  282. func (client Client) GetBucketLifecycle(bucketName string) (GetBucketLifecycleResult, error) {
  283. var out GetBucketLifecycleResult
  284. params := map[string]interface{}{}
  285. params["lifecycle"] = nil
  286. resp, err := client.do("GET", bucketName, params, nil, nil)
  287. if err != nil {
  288. return out, err
  289. }
  290. defer resp.Body.Close()
  291. err = xmlUnmarshal(resp.Body, &out)
  292. return out, err
  293. }
  294. // SetBucketReferer sets the bucket's referer whitelist and the flag if allowing empty referrer.
  295. //
  296. // To avoid stealing link on OSS data, OSS supports the HTTP referrer header. A whitelist referrer could be set either by API or web console, as well as
  297. // the allowing empty referrer flag. Note that this applies to requests from webbrowser only.
  298. // For example, for a bucket os-example and its referrer http://www.aliyun.com, all requests from this URL could access the bucket.
  299. // For more information, please check out this link :
  300. // https://help.aliyun.com/document_detail/oss/user_guide/security_management/referer.html
  301. //
  302. // bucketName the bucket name.
  303. // referers the referrer white list. A bucket could have a referrer list and each referrer supports one '*' and multiple '?' as wildcards.
  304. // The sample could be found in sample/bucket_referer.go
  305. // allowEmptyReferer the flag of allowing empty referrer. By default it's true.
  306. //
  307. // error it's nil if no error, otherwise it's an error object.
  308. //
  309. func (client Client) SetBucketReferer(bucketName string, referers []string, allowEmptyReferer bool) error {
  310. rxml := RefererXML{}
  311. rxml.AllowEmptyReferer = allowEmptyReferer
  312. if referers == nil {
  313. rxml.RefererList = append(rxml.RefererList, "")
  314. } else {
  315. for _, referer := range referers {
  316. rxml.RefererList = append(rxml.RefererList, referer)
  317. }
  318. }
  319. bs, err := xml.Marshal(rxml)
  320. if err != nil {
  321. return err
  322. }
  323. buffer := new(bytes.Buffer)
  324. buffer.Write(bs)
  325. contentType := http.DetectContentType(buffer.Bytes())
  326. headers := map[string]string{}
  327. headers[HTTPHeaderContentType] = contentType
  328. params := map[string]interface{}{}
  329. params["referer"] = nil
  330. resp, err := client.do("PUT", bucketName, params, headers, buffer)
  331. if err != nil {
  332. return err
  333. }
  334. defer resp.Body.Close()
  335. return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
  336. }
  337. // GetBucketReferer gets the bucket's referrer white list.
  338. //
  339. // bucketName the bucket name.
  340. //
  341. // GetBucketRefererResponse the result object upon successful request. It's only valid when error is nil.
  342. // error it's nil if no error, otherwise it's an error object.
  343. //
  344. func (client Client) GetBucketReferer(bucketName string) (GetBucketRefererResult, error) {
  345. var out GetBucketRefererResult
  346. params := map[string]interface{}{}
  347. params["referer"] = nil
  348. resp, err := client.do("GET", bucketName, params, nil, nil)
  349. if err != nil {
  350. return out, err
  351. }
  352. defer resp.Body.Close()
  353. err = xmlUnmarshal(resp.Body, &out)
  354. return out, err
  355. }
  356. // SetBucketLogging sets the bucket logging settings.
  357. //
  358. // OSS could automatically store the access log. Only the bucket owner could enable the logging.
  359. // Once enabled, OSS would save all the access log into hourly log files in a specified bucket.
  360. // For more information, please check out https://help.aliyun.com/document_detail/oss/user_guide/security_management/logging.html
  361. //
  362. // bucketName bucket name to enable the log.
  363. // targetBucket the target bucket name to store the log files.
  364. // targetPrefix the log files' prefix.
  365. //
  366. // error it's nil if no error, otherwise it's an error object.
  367. //
  368. func (client Client) SetBucketLogging(bucketName, targetBucket, targetPrefix string,
  369. isEnable bool) error {
  370. var err error
  371. var bs []byte
  372. if isEnable {
  373. lxml := LoggingXML{}
  374. lxml.LoggingEnabled.TargetBucket = targetBucket
  375. lxml.LoggingEnabled.TargetPrefix = targetPrefix
  376. bs, err = xml.Marshal(lxml)
  377. } else {
  378. lxml := loggingXMLEmpty{}
  379. bs, err = xml.Marshal(lxml)
  380. }
  381. if err != nil {
  382. return err
  383. }
  384. buffer := new(bytes.Buffer)
  385. buffer.Write(bs)
  386. contentType := http.DetectContentType(buffer.Bytes())
  387. headers := map[string]string{}
  388. headers[HTTPHeaderContentType] = contentType
  389. params := map[string]interface{}{}
  390. params["logging"] = nil
  391. resp, err := client.do("PUT", bucketName, params, headers, buffer)
  392. if err != nil {
  393. return err
  394. }
  395. defer resp.Body.Close()
  396. return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
  397. }
  398. // DeleteBucketLogging deletes the logging configuration to disable the logging on the bucket.
  399. //
  400. // bucketName the bucket name to disable the logging.
  401. //
  402. // error it's nil if no error, otherwise it's an error object.
  403. //
  404. func (client Client) DeleteBucketLogging(bucketName string) error {
  405. params := map[string]interface{}{}
  406. params["logging"] = nil
  407. resp, err := client.do("DELETE", bucketName, params, nil, nil)
  408. if err != nil {
  409. return err
  410. }
  411. defer resp.Body.Close()
  412. return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent})
  413. }
  414. // GetBucketLogging gets the bucket's logging settings
  415. //
  416. // bucketName the bucket name
  417. // GetBucketLoggingResponse the result object upon successful request. It's only valid when error is nil.
  418. //
  419. // error it's nil if no error, otherwise it's an error object.
  420. //
  421. func (client Client) GetBucketLogging(bucketName string) (GetBucketLoggingResult, error) {
  422. var out GetBucketLoggingResult
  423. params := map[string]interface{}{}
  424. params["logging"] = nil
  425. resp, err := client.do("GET", bucketName, params, nil, nil)
  426. if err != nil {
  427. return out, err
  428. }
  429. defer resp.Body.Close()
  430. err = xmlUnmarshal(resp.Body, &out)
  431. return out, err
  432. }
  433. // SetBucketWebsite sets the bucket's static website's index and error page.
  434. //
  435. // OSS supports static web site hosting for the bucket data. When the bucket is enabled with that, you can access the file in the bucket like the way to access a static website.
  436. // For more information, please check out: https://help.aliyun.com/document_detail/oss/user_guide/static_host_website.html
  437. //
  438. // bucketName the bucket name to enable static web site.
  439. // indexDocument index page.
  440. // errorDocument error page.
  441. //
  442. // error it's nil if no error, otherwise it's an error object.
  443. //
  444. func (client Client) SetBucketWebsite(bucketName, indexDocument, errorDocument string) error {
  445. wxml := WebsiteXML{}
  446. wxml.IndexDocument.Suffix = indexDocument
  447. wxml.ErrorDocument.Key = errorDocument
  448. bs, err := xml.Marshal(wxml)
  449. if err != nil {
  450. return err
  451. }
  452. buffer := new(bytes.Buffer)
  453. buffer.Write(bs)
  454. contentType := http.DetectContentType(buffer.Bytes())
  455. headers := make(map[string]string)
  456. headers[HTTPHeaderContentType] = contentType
  457. params := map[string]interface{}{}
  458. params["website"] = nil
  459. resp, err := client.do("PUT", bucketName, params, headers, buffer)
  460. if err != nil {
  461. return err
  462. }
  463. defer resp.Body.Close()
  464. return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
  465. }
  466. // SetBucketWebsiteDetail sets the bucket's static website's detail
  467. //
  468. // OSS supports static web site hosting for the bucket data. When the bucket is enabled with that, you can access the file in the bucket like the way to access a static website.
  469. // For more information, please check out: https://help.aliyun.com/document_detail/oss/user_guide/static_host_website.html
  470. //
  471. // bucketName the bucket name to enable static web site.
  472. //
  473. // wxml the website's detail
  474. //
  475. // error it's nil if no error, otherwise it's an error object.
  476. //
  477. func (client Client) SetBucketWebsiteDetail(bucketName string, wxml WebsiteXML, options ...Option) error {
  478. bs, err := xml.Marshal(wxml)
  479. if err != nil {
  480. return err
  481. }
  482. buffer := new(bytes.Buffer)
  483. buffer.Write(bs)
  484. contentType := http.DetectContentType(buffer.Bytes())
  485. headers := make(map[string]string)
  486. headers[HTTPHeaderContentType] = contentType
  487. params := map[string]interface{}{}
  488. params["website"] = nil
  489. resp, err := client.do("PUT", bucketName, params, headers, buffer, options...)
  490. if err != nil {
  491. return err
  492. }
  493. defer resp.Body.Close()
  494. return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
  495. }
  496. // DeleteBucketWebsite deletes the bucket's static web site settings.
  497. //
  498. // bucketName the bucket name.
  499. //
  500. // error it's nil if no error, otherwise it's an error object.
  501. //
  502. func (client Client) DeleteBucketWebsite(bucketName string) error {
  503. params := map[string]interface{}{}
  504. params["website"] = nil
  505. resp, err := client.do("DELETE", bucketName, params, nil, nil)
  506. if err != nil {
  507. return err
  508. }
  509. defer resp.Body.Close()
  510. return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent})
  511. }
  512. // GetBucketWebsite gets the bucket's default page (index page) and the error page.
  513. //
  514. // bucketName the bucket name
  515. //
  516. // GetBucketWebsiteResponse the result object upon successful request. It's only valid when error is nil.
  517. // error it's nil if no error, otherwise it's an error object.
  518. //
  519. func (client Client) GetBucketWebsite(bucketName string) (GetBucketWebsiteResult, error) {
  520. var out GetBucketWebsiteResult
  521. params := map[string]interface{}{}
  522. params["website"] = nil
  523. resp, err := client.do("GET", bucketName, params, nil, nil)
  524. if err != nil {
  525. return out, err
  526. }
  527. defer resp.Body.Close()
  528. err = xmlUnmarshal(resp.Body, &out)
  529. return out, err
  530. }
  531. // SetBucketCORS sets the bucket's CORS rules
  532. //
  533. // For more information, please check out https://help.aliyun.com/document_detail/oss/user_guide/security_management/cors.html
  534. //
  535. // bucketName the bucket name
  536. // corsRules the CORS rules to set. The related sample code is in sample/bucket_cors.go.
  537. //
  538. // error it's nil if no error, otherwise it's an error object.
  539. //
  540. func (client Client) SetBucketCORS(bucketName string, corsRules []CORSRule) error {
  541. corsxml := CORSXML{}
  542. for _, v := range corsRules {
  543. cr := CORSRule{}
  544. cr.AllowedMethod = v.AllowedMethod
  545. cr.AllowedOrigin = v.AllowedOrigin
  546. cr.AllowedHeader = v.AllowedHeader
  547. cr.ExposeHeader = v.ExposeHeader
  548. cr.MaxAgeSeconds = v.MaxAgeSeconds
  549. corsxml.CORSRules = append(corsxml.CORSRules, cr)
  550. }
  551. bs, err := xml.Marshal(corsxml)
  552. if err != nil {
  553. return err
  554. }
  555. buffer := new(bytes.Buffer)
  556. buffer.Write(bs)
  557. contentType := http.DetectContentType(buffer.Bytes())
  558. headers := map[string]string{}
  559. headers[HTTPHeaderContentType] = contentType
  560. params := map[string]interface{}{}
  561. params["cors"] = nil
  562. resp, err := client.do("PUT", bucketName, params, headers, buffer)
  563. if err != nil {
  564. return err
  565. }
  566. defer resp.Body.Close()
  567. return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
  568. }
  569. // DeleteBucketCORS deletes the bucket's static website settings.
  570. //
  571. // bucketName the bucket name.
  572. //
  573. // error it's nil if no error, otherwise it's an error object.
  574. //
  575. func (client Client) DeleteBucketCORS(bucketName string) error {
  576. params := map[string]interface{}{}
  577. params["cors"] = nil
  578. resp, err := client.do("DELETE", bucketName, params, nil, nil)
  579. if err != nil {
  580. return err
  581. }
  582. defer resp.Body.Close()
  583. return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent})
  584. }
  585. // GetBucketCORS gets the bucket's CORS settings.
  586. //
  587. // bucketName the bucket name.
  588. // GetBucketCORSResult the result object upon successful request. It's only valid when error is nil.
  589. //
  590. // error it's nil if no error, otherwise it's an error object.
  591. //
  592. func (client Client) GetBucketCORS(bucketName string) (GetBucketCORSResult, error) {
  593. var out GetBucketCORSResult
  594. params := map[string]interface{}{}
  595. params["cors"] = nil
  596. resp, err := client.do("GET", bucketName, params, nil, nil)
  597. if err != nil {
  598. return out, err
  599. }
  600. defer resp.Body.Close()
  601. err = xmlUnmarshal(resp.Body, &out)
  602. return out, err
  603. }
  604. // GetBucketInfo gets the bucket information.
  605. //
  606. // bucketName the bucket name.
  607. // GetBucketInfoResult the result object upon successful request. It's only valid when error is nil.
  608. //
  609. // error it's nil if no error, otherwise it's an error object.
  610. //
  611. func (client Client) GetBucketInfo(bucketName string, options ...Option) (GetBucketInfoResult, error) {
  612. var out GetBucketInfoResult
  613. params := map[string]interface{}{}
  614. params["bucketInfo"] = nil
  615. resp, err := client.do("GET", bucketName, params, nil, nil, options...)
  616. if err != nil {
  617. return out, err
  618. }
  619. defer resp.Body.Close()
  620. err = xmlUnmarshal(resp.Body, &out)
  621. // convert None to ""
  622. if err == nil {
  623. if out.BucketInfo.SseRule.KMSMasterKeyID == "None" {
  624. out.BucketInfo.SseRule.KMSMasterKeyID = ""
  625. }
  626. if out.BucketInfo.SseRule.SSEAlgorithm == "None" {
  627. out.BucketInfo.SseRule.SSEAlgorithm = ""
  628. }
  629. }
  630. return out, err
  631. }
  632. // SetBucketVersioning set bucket versioning:Enabled、Suspended
  633. // bucketName the bucket name.
  634. // error it's nil if no error, otherwise it's an error object.
  635. func (client Client) SetBucketVersioning(bucketName string, versioningConfig VersioningConfig, options ...Option) error {
  636. var err error
  637. var bs []byte
  638. bs, err = xml.Marshal(versioningConfig)
  639. if err != nil {
  640. return err
  641. }
  642. buffer := new(bytes.Buffer)
  643. buffer.Write(bs)
  644. contentType := http.DetectContentType(buffer.Bytes())
  645. headers := map[string]string{}
  646. headers[HTTPHeaderContentType] = contentType
  647. params := map[string]interface{}{}
  648. params["versioning"] = nil
  649. resp, err := client.do("PUT", bucketName, params, headers, buffer, options...)
  650. if err != nil {
  651. return err
  652. }
  653. defer resp.Body.Close()
  654. return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
  655. }
  656. // GetBucketVersioning get bucket versioning status:Enabled、Suspended
  657. // bucketName the bucket name.
  658. // error it's nil if no error, otherwise it's an error object.
  659. func (client Client) GetBucketVersioning(bucketName string, options ...Option) (GetBucketVersioningResult, error) {
  660. var out GetBucketVersioningResult
  661. params := map[string]interface{}{}
  662. params["versioning"] = nil
  663. resp, err := client.do("GET", bucketName, params, nil, nil, options...)
  664. if err != nil {
  665. return out, err
  666. }
  667. defer resp.Body.Close()
  668. err = xmlUnmarshal(resp.Body, &out)
  669. return out, err
  670. }
  671. // SetBucketEncryption set bucket encryption config
  672. // bucketName the bucket name.
  673. // error it's nil if no error, otherwise it's an error object.
  674. func (client Client) SetBucketEncryption(bucketName string, encryptionRule ServerEncryptionRule, options ...Option) error {
  675. var err error
  676. var bs []byte
  677. bs, err = xml.Marshal(encryptionRule)
  678. if err != nil {
  679. return err
  680. }
  681. buffer := new(bytes.Buffer)
  682. buffer.Write(bs)
  683. contentType := http.DetectContentType(buffer.Bytes())
  684. headers := map[string]string{}
  685. headers[HTTPHeaderContentType] = contentType
  686. params := map[string]interface{}{}
  687. params["encryption"] = nil
  688. resp, err := client.do("PUT", bucketName, params, headers, buffer, options...)
  689. if err != nil {
  690. return err
  691. }
  692. defer resp.Body.Close()
  693. return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
  694. }
  695. // GetBucketEncryption get bucket encryption
  696. // bucketName the bucket name.
  697. // error it's nil if no error, otherwise it's an error object.
  698. func (client Client) GetBucketEncryption(bucketName string, options ...Option) (GetBucketEncryptionResult, error) {
  699. var out GetBucketEncryptionResult
  700. params := map[string]interface{}{}
  701. params["encryption"] = nil
  702. resp, err := client.do("GET", bucketName, params, nil, nil, options...)
  703. if err != nil {
  704. return out, err
  705. }
  706. defer resp.Body.Close()
  707. err = xmlUnmarshal(resp.Body, &out)
  708. return out, err
  709. }
  710. // DeleteBucketEncryption delete bucket encryption config
  711. // bucketName the bucket name.
  712. // error it's nil if no error, otherwise it's an error bucket
  713. func (client Client) DeleteBucketEncryption(bucketName string, options ...Option) error {
  714. params := map[string]interface{}{}
  715. params["encryption"] = nil
  716. resp, err := client.do("DELETE", bucketName, params, nil, nil, options...)
  717. if err != nil {
  718. return err
  719. }
  720. defer resp.Body.Close()
  721. return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent})
  722. }
  723. //
  724. // SetBucketTagging add tagging to bucket
  725. // bucketName name of bucket
  726. // tagging tagging to be added
  727. // error nil if success, otherwise error
  728. func (client Client) SetBucketTagging(bucketName string, tagging Tagging, options ...Option) error {
  729. var err error
  730. var bs []byte
  731. bs, err = xml.Marshal(tagging)
  732. if err != nil {
  733. return err
  734. }
  735. buffer := new(bytes.Buffer)
  736. buffer.Write(bs)
  737. contentType := http.DetectContentType(buffer.Bytes())
  738. headers := map[string]string{}
  739. headers[HTTPHeaderContentType] = contentType
  740. params := map[string]interface{}{}
  741. params["tagging"] = nil
  742. resp, err := client.do("PUT", bucketName, params, headers, buffer, options...)
  743. if err != nil {
  744. return err
  745. }
  746. defer resp.Body.Close()
  747. return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
  748. }
  749. // GetBucketTagging get tagging of the bucket
  750. // bucketName name of bucket
  751. // error nil if success, otherwise error
  752. func (client Client) GetBucketTagging(bucketName string, options ...Option) (GetBucketTaggingResult, error) {
  753. var out GetBucketTaggingResult
  754. params := map[string]interface{}{}
  755. params["tagging"] = nil
  756. resp, err := client.do("GET", bucketName, params, nil, nil, options...)
  757. if err != nil {
  758. return out, err
  759. }
  760. defer resp.Body.Close()
  761. err = xmlUnmarshal(resp.Body, &out)
  762. return out, err
  763. }
  764. //
  765. // DeleteBucketTagging delete bucket tagging
  766. // bucketName name of bucket
  767. // error nil if success, otherwise error
  768. //
  769. func (client Client) DeleteBucketTagging(bucketName string, options ...Option) error {
  770. params := map[string]interface{}{}
  771. params["tagging"] = nil
  772. resp, err := client.do("DELETE", bucketName, params, nil, nil, options...)
  773. if err != nil {
  774. return err
  775. }
  776. defer resp.Body.Close()
  777. return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent})
  778. }
  779. // GetBucketStat get bucket stat
  780. // bucketName the bucket name.
  781. // error it's nil if no error, otherwise it's an error object.
  782. func (client Client) GetBucketStat(bucketName string) (GetBucketStatResult, error) {
  783. var out GetBucketStatResult
  784. params := map[string]interface{}{}
  785. params["stat"] = nil
  786. resp, err := client.do("GET", bucketName, params, nil, nil)
  787. if err != nil {
  788. return out, err
  789. }
  790. defer resp.Body.Close()
  791. err = xmlUnmarshal(resp.Body, &out)
  792. return out, err
  793. }
  794. // GetBucketPolicy API operation for Object Storage Service.
  795. //
  796. // Get the policy from the bucket.
  797. //
  798. // bucketName the bucket name.
  799. //
  800. // string return the bucket's policy, and it's only valid when error is nil.
  801. //
  802. // error it's nil if no error, otherwise it's an error object.
  803. //
  804. func (client Client) GetBucketPolicy(bucketName string, options ...Option) (string, error) {
  805. params := map[string]interface{}{}
  806. params["policy"] = nil
  807. resp, err := client.do("GET", bucketName, params, nil, nil, options...)
  808. if err != nil {
  809. return "", err
  810. }
  811. defer resp.Body.Close()
  812. body, err := ioutil.ReadAll(resp.Body)
  813. out := string(body)
  814. return out, err
  815. }
  816. // SetBucketPolicy API operation for Object Storage Service.
  817. //
  818. // Set the policy from the bucket.
  819. //
  820. // bucketName the bucket name.
  821. //
  822. // policy the bucket policy.
  823. //
  824. // error it's nil if no error, otherwise it's an error object.
  825. //
  826. func (client Client) SetBucketPolicy(bucketName string, policy string, options ...Option) error {
  827. params := map[string]interface{}{}
  828. params["policy"] = nil
  829. buffer := strings.NewReader(policy)
  830. resp, err := client.do("PUT", bucketName, params, nil, buffer, options...)
  831. if err != nil {
  832. return err
  833. }
  834. defer resp.Body.Close()
  835. return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
  836. }
  837. // DeleteBucketPolicy API operation for Object Storage Service.
  838. //
  839. // Deletes the policy from the bucket.
  840. //
  841. // bucketName the bucket name.
  842. //
  843. // error it's nil if no error, otherwise it's an error object.
  844. //
  845. func (client Client) DeleteBucketPolicy(bucketName string, options ...Option) error {
  846. params := map[string]interface{}{}
  847. params["policy"] = nil
  848. resp, err := client.do("DELETE", bucketName, params, nil, nil, options...)
  849. if err != nil {
  850. return err
  851. }
  852. defer resp.Body.Close()
  853. return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent})
  854. }
  855. // SetBucketRequestPayment API operation for Object Storage Service.
  856. //
  857. // Set the requestPayment of bucket
  858. //
  859. // bucketName the bucket name.
  860. //
  861. // paymentConfig the payment configuration
  862. //
  863. // error it's nil if no error, otherwise it's an error object.
  864. //
  865. func (client Client) SetBucketRequestPayment(bucketName string, paymentConfig RequestPaymentConfiguration, options ...Option) error {
  866. params := map[string]interface{}{}
  867. params["requestPayment"] = nil
  868. var bs []byte
  869. bs, err := xml.Marshal(paymentConfig)
  870. if err != nil {
  871. return err
  872. }
  873. buffer := new(bytes.Buffer)
  874. buffer.Write(bs)
  875. contentType := http.DetectContentType(buffer.Bytes())
  876. headers := map[string]string{}
  877. headers[HTTPHeaderContentType] = contentType
  878. resp, err := client.do("PUT", bucketName, params, headers, buffer, options...)
  879. if err != nil {
  880. return err
  881. }
  882. defer resp.Body.Close()
  883. return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
  884. }
  885. // GetBucketRequestPayment API operation for Object Storage Service.
  886. //
  887. // Get bucket requestPayment
  888. //
  889. // bucketName the bucket name.
  890. //
  891. // RequestPaymentConfiguration the payment configuration
  892. //
  893. // error it's nil if no error, otherwise it's an error object.
  894. //
  895. func (client Client) GetBucketRequestPayment(bucketName string, options ...Option) (RequestPaymentConfiguration, error) {
  896. var out RequestPaymentConfiguration
  897. params := map[string]interface{}{}
  898. params["requestPayment"] = nil
  899. resp, err := client.do("GET", bucketName, params, nil, nil, options...)
  900. if err != nil {
  901. return out, err
  902. }
  903. defer resp.Body.Close()
  904. err = xmlUnmarshal(resp.Body, &out)
  905. return out, err
  906. }
  907. // GetUserQoSInfo API operation for Object Storage Service.
  908. //
  909. // Get user qos.
  910. //
  911. // UserQoSConfiguration the User Qos and range Information.
  912. //
  913. // error it's nil if no error, otherwise it's an error object.
  914. //
  915. func (client Client) GetUserQoSInfo(options ...Option) (UserQoSConfiguration, error) {
  916. var out UserQoSConfiguration
  917. params := map[string]interface{}{}
  918. params["qosInfo"] = nil
  919. resp, err := client.do("GET", "", params, nil, nil, options...)
  920. if err != nil {
  921. return out, err
  922. }
  923. defer resp.Body.Close()
  924. err = xmlUnmarshal(resp.Body, &out)
  925. return out, err
  926. }
  927. // SetBucketQoSInfo API operation for Object Storage Service.
  928. //
  929. // Set Bucket Qos information.
  930. //
  931. // bucketName tht bucket name.
  932. //
  933. // qosConf the qos configuration.
  934. //
  935. // error it's nil if no error, otherwise it's an error object.
  936. //
  937. func (client Client) SetBucketQoSInfo(bucketName string, qosConf BucketQoSConfiguration, options ...Option) error {
  938. params := map[string]interface{}{}
  939. params["qosInfo"] = nil
  940. var bs []byte
  941. bs, err := xml.Marshal(qosConf)
  942. if err != nil {
  943. return err
  944. }
  945. buffer := new(bytes.Buffer)
  946. buffer.Write(bs)
  947. contentTpye := http.DetectContentType(buffer.Bytes())
  948. headers := map[string]string{}
  949. headers[HTTPHeaderContentType] = contentTpye
  950. resp, err := client.do("PUT", bucketName, params, headers, buffer, options...)
  951. if err != nil {
  952. return err
  953. }
  954. defer resp.Body.Close()
  955. return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
  956. }
  957. // GetBucketQosInfo API operation for Object Storage Service.
  958. //
  959. // Get Bucket Qos information.
  960. //
  961. // bucketName tht bucket name.
  962. //
  963. // BucketQoSConfiguration the return qos configuration.
  964. //
  965. // error it's nil if no error, otherwise it's an error object.
  966. //
  967. func (client Client) GetBucketQosInfo(bucketName string, options ...Option) (BucketQoSConfiguration, error) {
  968. var out BucketQoSConfiguration
  969. params := map[string]interface{}{}
  970. params["qosInfo"] = nil
  971. resp, err := client.do("GET", bucketName, params, nil, nil, options...)
  972. if err != nil {
  973. return out, err
  974. }
  975. defer resp.Body.Close()
  976. err = xmlUnmarshal(resp.Body, &out)
  977. return out, err
  978. }
  979. // DeleteBucketQosInfo API operation for Object Storage Service.
  980. //
  981. // Delete Bucket QoS information.
  982. //
  983. // bucketName tht bucket name.
  984. //
  985. // error it's nil if no error, otherwise it's an error object.
  986. //
  987. func (client Client) DeleteBucketQosInfo(bucketName string, options ...Option) error {
  988. params := map[string]interface{}{}
  989. params["qosInfo"] = nil
  990. resp, err := client.do("DELETE", bucketName, params, nil, nil, options...)
  991. if err != nil {
  992. return err
  993. }
  994. defer resp.Body.Close()
  995. return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent})
  996. }
  997. // LimitUploadSpeed set upload bandwidth limit speed,default is 0,unlimited
  998. // upSpeed KB/s, 0 is unlimited,default is 0
  999. // error it's nil if success, otherwise failure
  1000. func (client Client) LimitUploadSpeed(upSpeed int) error {
  1001. if client.Config == nil {
  1002. return fmt.Errorf("client config is nil")
  1003. }
  1004. return client.Config.LimitUploadSpeed(upSpeed)
  1005. }
  1006. // UseCname sets the flag of using CName. By default it's false.
  1007. //
  1008. // isUseCname true: the endpoint has the CName, false: the endpoint does not have cname. Default is false.
  1009. //
  1010. func UseCname(isUseCname bool) ClientOption {
  1011. return func(client *Client) {
  1012. client.Config.IsCname = isUseCname
  1013. client.Conn.url.Init(client.Config.Endpoint, client.Config.IsCname, client.Config.IsUseProxy)
  1014. }
  1015. }
  1016. // Timeout sets the HTTP timeout in seconds.
  1017. //
  1018. // connectTimeoutSec HTTP timeout in seconds. Default is 10 seconds. 0 means infinite (not recommended)
  1019. // readWriteTimeout HTTP read or write's timeout in seconds. Default is 20 seconds. 0 means infinite.
  1020. //
  1021. func Timeout(connectTimeoutSec, readWriteTimeout int64) ClientOption {
  1022. return func(client *Client) {
  1023. client.Config.HTTPTimeout.ConnectTimeout =
  1024. time.Second * time.Duration(connectTimeoutSec)
  1025. client.Config.HTTPTimeout.ReadWriteTimeout =
  1026. time.Second * time.Duration(readWriteTimeout)
  1027. client.Config.HTTPTimeout.HeaderTimeout =
  1028. time.Second * time.Duration(readWriteTimeout)
  1029. client.Config.HTTPTimeout.IdleConnTimeout =
  1030. time.Second * time.Duration(readWriteTimeout)
  1031. client.Config.HTTPTimeout.LongTimeout =
  1032. time.Second * time.Duration(readWriteTimeout*10)
  1033. }
  1034. }
  1035. // SecurityToken sets the temporary user's SecurityToken.
  1036. //
  1037. // token STS token
  1038. //
  1039. func SecurityToken(token string) ClientOption {
  1040. return func(client *Client) {
  1041. client.Config.SecurityToken = strings.TrimSpace(token)
  1042. }
  1043. }
  1044. // EnableMD5 enables MD5 validation.
  1045. //
  1046. // isEnableMD5 true: enable MD5 validation; false: disable MD5 validation.
  1047. //
  1048. func EnableMD5(isEnableMD5 bool) ClientOption {
  1049. return func(client *Client) {
  1050. client.Config.IsEnableMD5 = isEnableMD5
  1051. }
  1052. }
  1053. // MD5ThresholdCalcInMemory sets the memory usage threshold for computing the MD5, default is 16MB.
  1054. //
  1055. // threshold the memory threshold in bytes. When the uploaded content is more than 16MB, the temp file is used for computing the MD5.
  1056. //
  1057. func MD5ThresholdCalcInMemory(threshold int64) ClientOption {
  1058. return func(client *Client) {
  1059. client.Config.MD5Threshold = threshold
  1060. }
  1061. }
  1062. // EnableCRC enables the CRC checksum. Default is true.
  1063. //
  1064. // isEnableCRC true: enable CRC checksum; false: disable the CRC checksum.
  1065. //
  1066. func EnableCRC(isEnableCRC bool) ClientOption {
  1067. return func(client *Client) {
  1068. client.Config.IsEnableCRC = isEnableCRC
  1069. }
  1070. }
  1071. // UserAgent specifies UserAgent. The default is aliyun-sdk-go/1.2.0 (windows/-/amd64;go1.5.2).
  1072. //
  1073. // userAgent the user agent string.
  1074. //
  1075. func UserAgent(userAgent string) ClientOption {
  1076. return func(client *Client) {
  1077. client.Config.UserAgent = userAgent
  1078. client.Config.UserSetUa = true
  1079. }
  1080. }
  1081. // Proxy sets the proxy (optional). The default is not using proxy.
  1082. //
  1083. // proxyHost the proxy host in the format "host:port". For example, proxy.com:80 .
  1084. //
  1085. func Proxy(proxyHost string) ClientOption {
  1086. return func(client *Client) {
  1087. client.Config.IsUseProxy = true
  1088. client.Config.ProxyHost = proxyHost
  1089. client.Conn.url.Init(client.Config.Endpoint, client.Config.IsCname, client.Config.IsUseProxy)
  1090. }
  1091. }
  1092. // AuthProxy sets the proxy information with user name and password.
  1093. //
  1094. // proxyHost the proxy host in the format "host:port". For example, proxy.com:80 .
  1095. // proxyUser the proxy user name.
  1096. // proxyPassword the proxy password.
  1097. //
  1098. func AuthProxy(proxyHost, proxyUser, proxyPassword string) ClientOption {
  1099. return func(client *Client) {
  1100. client.Config.IsUseProxy = true
  1101. client.Config.ProxyHost = proxyHost
  1102. client.Config.IsAuthProxy = true
  1103. client.Config.ProxyUser = proxyUser
  1104. client.Config.ProxyPassword = proxyPassword
  1105. client.Conn.url.Init(client.Config.Endpoint, client.Config.IsCname, client.Config.IsUseProxy)
  1106. }
  1107. }
  1108. //
  1109. // HTTPClient sets the http.Client in use to the one passed in
  1110. //
  1111. func HTTPClient(HTTPClient *http.Client) ClientOption {
  1112. return func(client *Client) {
  1113. client.HTTPClient = HTTPClient
  1114. }
  1115. }
  1116. //
  1117. // SetLogLevel sets the oss sdk log level
  1118. //
  1119. func SetLogLevel(LogLevel int) ClientOption {
  1120. return func(client *Client) {
  1121. client.Config.LogLevel = LogLevel
  1122. }
  1123. }
  1124. //
  1125. // SetLogger sets the oss sdk logger
  1126. //
  1127. func SetLogger(Logger *log.Logger) ClientOption {
  1128. return func(client *Client) {
  1129. client.Config.Logger = Logger
  1130. }
  1131. }
  1132. // SetCredentialsProvider sets funciton for get the user's ak
  1133. func SetCredentialsProvider(provider CredentialsProvider) ClientOption {
  1134. return func(client *Client) {
  1135. client.Config.CredentialsProvider = provider
  1136. }
  1137. }
  1138. // SetLocalAddr sets funciton for local addr
  1139. func SetLocalAddr(localAddr net.Addr) ClientOption {
  1140. return func(client *Client) {
  1141. client.Config.LocalAddr = localAddr
  1142. }
  1143. }
  1144. // Private
  1145. func (client Client) do(method, bucketName string, params map[string]interface{},
  1146. headers map[string]string, data io.Reader, options ...Option) (*Response, error) {
  1147. resp, err := client.Conn.Do(method, bucketName, "", params, headers, data, 0, nil)
  1148. // get response header
  1149. respHeader, _ := FindOption(options, responseHeader, nil)
  1150. if respHeader != nil {
  1151. pRespHeader := respHeader.(*http.Header)
  1152. *pRespHeader = resp.Headers
  1153. }
  1154. return resp, err
  1155. }