client.go 39 KB

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