client.go 38 KB

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