client.go 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  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. // SetBucketWebsiteXml sets the bucket's static website's rule
  510. //
  511. // 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.
  512. // For more information, please check out: https://help.aliyun.com/document_detail/oss/user_guide/static_host_website.html
  513. //
  514. // bucketName the bucket name to enable static web site.
  515. //
  516. // wxml the website's detail
  517. //
  518. // error it's nil if no error, otherwise it's an error object.
  519. //
  520. func (client Client) SetBucketWebsiteXml(bucketName string, webXml string, options ...Option) error {
  521. buffer := new(bytes.Buffer)
  522. buffer.Write([]byte(webXml))
  523. contentType := http.DetectContentType(buffer.Bytes())
  524. headers := make(map[string]string)
  525. headers[HTTPHeaderContentType] = contentType
  526. params := map[string]interface{}{}
  527. params["website"] = nil
  528. resp, err := client.do("PUT", bucketName, params, headers, buffer, options...)
  529. if err != nil {
  530. return err
  531. }
  532. defer resp.Body.Close()
  533. return checkRespCode(resp.StatusCode, []int{http.StatusOK})
  534. }
  535. // DeleteBucketWebsite deletes the bucket's static web site settings.
  536. //
  537. // bucketName the bucket name.
  538. //
  539. // error it's nil if no error, otherwise it's an error object.
  540. //
  541. func (client Client) DeleteBucketWebsite(bucketName string) error {
  542. params := map[string]interface{}{}
  543. params["website"] = nil
  544. resp, err := client.do("DELETE", bucketName, params, nil, nil)
  545. if err != nil {
  546. return err
  547. }
  548. defer resp.Body.Close()
  549. return checkRespCode(resp.StatusCode, []int{http.StatusNoContent})
  550. }
  551. // GetBucketWebsite gets the bucket's default page (index page) and the error page.
  552. //
  553. // bucketName the bucket name
  554. //
  555. // GetBucketWebsiteResponse the result object upon successful request. It's only valid when error is nil.
  556. // error it's nil if no error, otherwise it's an error object.
  557. //
  558. func (client Client) GetBucketWebsite(bucketName string) (GetBucketWebsiteResult, error) {
  559. var out GetBucketWebsiteResult
  560. params := map[string]interface{}{}
  561. params["website"] = nil
  562. resp, err := client.do("GET", bucketName, params, nil, nil)
  563. if err != nil {
  564. return out, err
  565. }
  566. defer resp.Body.Close()
  567. err = xmlUnmarshal(resp.Body, &out)
  568. return out, err
  569. }
  570. // SetBucketCORS sets the bucket's CORS rules
  571. //
  572. // For more information, please check out https://help.aliyun.com/document_detail/oss/user_guide/security_management/cors.html
  573. //
  574. // bucketName the bucket name
  575. // corsRules the CORS rules to set. The related sample code is in sample/bucket_cors.go.
  576. //
  577. // error it's nil if no error, otherwise it's an error object.
  578. //
  579. func (client Client) SetBucketCORS(bucketName string, corsRules []CORSRule) error {
  580. corsxml := CORSXML{}
  581. for _, v := range corsRules {
  582. cr := CORSRule{}
  583. cr.AllowedMethod = v.AllowedMethod
  584. cr.AllowedOrigin = v.AllowedOrigin
  585. cr.AllowedHeader = v.AllowedHeader
  586. cr.ExposeHeader = v.ExposeHeader
  587. cr.MaxAgeSeconds = v.MaxAgeSeconds
  588. corsxml.CORSRules = append(corsxml.CORSRules, cr)
  589. }
  590. bs, err := xml.Marshal(corsxml)
  591. if err != nil {
  592. return err
  593. }
  594. buffer := new(bytes.Buffer)
  595. buffer.Write(bs)
  596. contentType := http.DetectContentType(buffer.Bytes())
  597. headers := map[string]string{}
  598. headers[HTTPHeaderContentType] = contentType
  599. params := map[string]interface{}{}
  600. params["cors"] = nil
  601. resp, err := client.do("PUT", bucketName, params, headers, buffer)
  602. if err != nil {
  603. return err
  604. }
  605. defer resp.Body.Close()
  606. return checkRespCode(resp.StatusCode, []int{http.StatusOK})
  607. }
  608. // DeleteBucketCORS deletes the bucket's static website settings.
  609. //
  610. // bucketName the bucket name.
  611. //
  612. // error it's nil if no error, otherwise it's an error object.
  613. //
  614. func (client Client) DeleteBucketCORS(bucketName string) error {
  615. params := map[string]interface{}{}
  616. params["cors"] = nil
  617. resp, err := client.do("DELETE", bucketName, params, nil, nil)
  618. if err != nil {
  619. return err
  620. }
  621. defer resp.Body.Close()
  622. return checkRespCode(resp.StatusCode, []int{http.StatusNoContent})
  623. }
  624. // GetBucketCORS gets the bucket's CORS settings.
  625. //
  626. // bucketName the bucket name.
  627. // GetBucketCORSResult the result object upon successful request. It's only valid when error is nil.
  628. //
  629. // error it's nil if no error, otherwise it's an error object.
  630. //
  631. func (client Client) GetBucketCORS(bucketName string) (GetBucketCORSResult, error) {
  632. var out GetBucketCORSResult
  633. params := map[string]interface{}{}
  634. params["cors"] = nil
  635. resp, err := client.do("GET", bucketName, params, nil, nil)
  636. if err != nil {
  637. return out, err
  638. }
  639. defer resp.Body.Close()
  640. err = xmlUnmarshal(resp.Body, &out)
  641. return out, err
  642. }
  643. // GetBucketInfo gets the bucket information.
  644. //
  645. // bucketName the bucket name.
  646. // GetBucketInfoResult the result object upon successful request. It's only valid when error is nil.
  647. //
  648. // error it's nil if no error, otherwise it's an error object.
  649. //
  650. func (client Client) GetBucketInfo(bucketName string, options ...Option) (GetBucketInfoResult, error) {
  651. var out GetBucketInfoResult
  652. params := map[string]interface{}{}
  653. params["bucketInfo"] = nil
  654. resp, err := client.do("GET", bucketName, params, nil, nil, options...)
  655. if err != nil {
  656. return out, err
  657. }
  658. defer resp.Body.Close()
  659. err = xmlUnmarshal(resp.Body, &out)
  660. // convert None to ""
  661. if err == nil {
  662. if out.BucketInfo.SseRule.KMSMasterKeyID == "None" {
  663. out.BucketInfo.SseRule.KMSMasterKeyID = ""
  664. }
  665. if out.BucketInfo.SseRule.SSEAlgorithm == "None" {
  666. out.BucketInfo.SseRule.SSEAlgorithm = ""
  667. }
  668. }
  669. return out, err
  670. }
  671. // SetBucketVersioning set bucket versioning:Enabled、Suspended
  672. // bucketName the bucket name.
  673. // error it's nil if no error, otherwise it's an error object.
  674. func (client Client) SetBucketVersioning(bucketName string, versioningConfig VersioningConfig, options ...Option) error {
  675. var err error
  676. var bs []byte
  677. bs, err = xml.Marshal(versioningConfig)
  678. if err != nil {
  679. return err
  680. }
  681. buffer := new(bytes.Buffer)
  682. buffer.Write(bs)
  683. contentType := http.DetectContentType(buffer.Bytes())
  684. headers := map[string]string{}
  685. headers[HTTPHeaderContentType] = contentType
  686. params := map[string]interface{}{}
  687. params["versioning"] = nil
  688. resp, err := client.do("PUT", bucketName, params, headers, buffer, options...)
  689. if err != nil {
  690. return err
  691. }
  692. defer resp.Body.Close()
  693. return checkRespCode(resp.StatusCode, []int{http.StatusOK})
  694. }
  695. // GetBucketVersioning get bucket versioning status:Enabled、Suspended
  696. // bucketName the bucket name.
  697. // error it's nil if no error, otherwise it's an error object.
  698. func (client Client) GetBucketVersioning(bucketName string, options ...Option) (GetBucketVersioningResult, error) {
  699. var out GetBucketVersioningResult
  700. params := map[string]interface{}{}
  701. params["versioning"] = nil
  702. resp, err := client.do("GET", bucketName, params, nil, nil, options...)
  703. if err != nil {
  704. return out, err
  705. }
  706. defer resp.Body.Close()
  707. err = xmlUnmarshal(resp.Body, &out)
  708. return out, err
  709. }
  710. // SetBucketEncryption set bucket encryption config
  711. // bucketName the bucket name.
  712. // error it's nil if no error, otherwise it's an error object.
  713. func (client Client) SetBucketEncryption(bucketName string, encryptionRule ServerEncryptionRule, options ...Option) error {
  714. var err error
  715. var bs []byte
  716. bs, err = xml.Marshal(encryptionRule)
  717. if err != nil {
  718. return err
  719. }
  720. buffer := new(bytes.Buffer)
  721. buffer.Write(bs)
  722. contentType := http.DetectContentType(buffer.Bytes())
  723. headers := map[string]string{}
  724. headers[HTTPHeaderContentType] = contentType
  725. params := map[string]interface{}{}
  726. params["encryption"] = nil
  727. resp, err := client.do("PUT", bucketName, params, headers, buffer, options...)
  728. if err != nil {
  729. return err
  730. }
  731. defer resp.Body.Close()
  732. return checkRespCode(resp.StatusCode, []int{http.StatusOK})
  733. }
  734. // GetBucketEncryption get bucket encryption
  735. // bucketName the bucket name.
  736. // error it's nil if no error, otherwise it's an error object.
  737. func (client Client) GetBucketEncryption(bucketName string, options ...Option) (GetBucketEncryptionResult, error) {
  738. var out GetBucketEncryptionResult
  739. params := map[string]interface{}{}
  740. params["encryption"] = nil
  741. resp, err := client.do("GET", bucketName, params, nil, nil, options...)
  742. if err != nil {
  743. return out, err
  744. }
  745. defer resp.Body.Close()
  746. err = xmlUnmarshal(resp.Body, &out)
  747. return out, err
  748. }
  749. // DeleteBucketEncryption delete bucket encryption config
  750. // bucketName the bucket name.
  751. // error it's nil if no error, otherwise it's an error bucket
  752. func (client Client) DeleteBucketEncryption(bucketName string, options ...Option) error {
  753. params := map[string]interface{}{}
  754. params["encryption"] = nil
  755. resp, err := client.do("DELETE", bucketName, params, nil, nil, options...)
  756. if err != nil {
  757. return err
  758. }
  759. defer resp.Body.Close()
  760. return checkRespCode(resp.StatusCode, []int{http.StatusNoContent})
  761. }
  762. //
  763. // SetBucketTagging add tagging to bucket
  764. // bucketName name of bucket
  765. // tagging tagging to be added
  766. // error nil if success, otherwise error
  767. func (client Client) SetBucketTagging(bucketName string, tagging Tagging, options ...Option) error {
  768. var err error
  769. var bs []byte
  770. bs, err = xml.Marshal(tagging)
  771. if err != nil {
  772. return err
  773. }
  774. buffer := new(bytes.Buffer)
  775. buffer.Write(bs)
  776. contentType := http.DetectContentType(buffer.Bytes())
  777. headers := map[string]string{}
  778. headers[HTTPHeaderContentType] = contentType
  779. params := map[string]interface{}{}
  780. params["tagging"] = nil
  781. resp, err := client.do("PUT", bucketName, params, headers, buffer, options...)
  782. if err != nil {
  783. return err
  784. }
  785. defer resp.Body.Close()
  786. return checkRespCode(resp.StatusCode, []int{http.StatusOK})
  787. }
  788. // GetBucketTagging get tagging of the bucket
  789. // bucketName name of bucket
  790. // error nil if success, otherwise error
  791. func (client Client) GetBucketTagging(bucketName string, options ...Option) (GetBucketTaggingResult, error) {
  792. var out GetBucketTaggingResult
  793. params := map[string]interface{}{}
  794. params["tagging"] = nil
  795. resp, err := client.do("GET", bucketName, params, nil, nil, options...)
  796. if err != nil {
  797. return out, err
  798. }
  799. defer resp.Body.Close()
  800. err = xmlUnmarshal(resp.Body, &out)
  801. return out, err
  802. }
  803. //
  804. // DeleteBucketTagging delete bucket tagging
  805. // bucketName name of bucket
  806. // error nil if success, otherwise error
  807. //
  808. func (client Client) DeleteBucketTagging(bucketName string, options ...Option) error {
  809. params := map[string]interface{}{}
  810. params["tagging"] = nil
  811. resp, err := client.do("DELETE", bucketName, params, nil, nil, options...)
  812. if err != nil {
  813. return err
  814. }
  815. defer resp.Body.Close()
  816. return checkRespCode(resp.StatusCode, []int{http.StatusNoContent})
  817. }
  818. // GetBucketStat get bucket stat
  819. // bucketName the bucket name.
  820. // error it's nil if no error, otherwise it's an error object.
  821. func (client Client) GetBucketStat(bucketName string) (GetBucketStatResult, error) {
  822. var out GetBucketStatResult
  823. params := map[string]interface{}{}
  824. params["stat"] = nil
  825. resp, err := client.do("GET", bucketName, params, nil, nil)
  826. if err != nil {
  827. return out, err
  828. }
  829. defer resp.Body.Close()
  830. err = xmlUnmarshal(resp.Body, &out)
  831. return out, err
  832. }
  833. // GetBucketPolicy API operation for Object Storage Service.
  834. //
  835. // Get the policy from the bucket.
  836. //
  837. // bucketName the bucket name.
  838. //
  839. // string return the bucket's policy, and it's only valid when error is nil.
  840. //
  841. // error it's nil if no error, otherwise it's an error object.
  842. //
  843. func (client Client) GetBucketPolicy(bucketName string, options ...Option) (string, error) {
  844. params := map[string]interface{}{}
  845. params["policy"] = nil
  846. resp, err := client.do("GET", bucketName, params, nil, nil, options...)
  847. if err != nil {
  848. return "", err
  849. }
  850. defer resp.Body.Close()
  851. body, err := ioutil.ReadAll(resp.Body)
  852. out := string(body)
  853. return out, err
  854. }
  855. // SetBucketPolicy API operation for Object Storage Service.
  856. //
  857. // Set the policy from the bucket.
  858. //
  859. // bucketName the bucket name.
  860. //
  861. // policy the bucket policy.
  862. //
  863. // error it's nil if no error, otherwise it's an error object.
  864. //
  865. func (client Client) SetBucketPolicy(bucketName string, policy string, options ...Option) error {
  866. params := map[string]interface{}{}
  867. params["policy"] = nil
  868. buffer := strings.NewReader(policy)
  869. resp, err := client.do("PUT", bucketName, params, nil, buffer, options...)
  870. if err != nil {
  871. return err
  872. }
  873. defer resp.Body.Close()
  874. return checkRespCode(resp.StatusCode, []int{http.StatusOK})
  875. }
  876. // DeleteBucketPolicy API operation for Object Storage Service.
  877. //
  878. // Deletes the policy from the bucket.
  879. //
  880. // bucketName the bucket name.
  881. //
  882. // error it's nil if no error, otherwise it's an error object.
  883. //
  884. func (client Client) DeleteBucketPolicy(bucketName string, options ...Option) error {
  885. params := map[string]interface{}{}
  886. params["policy"] = nil
  887. resp, err := client.do("DELETE", bucketName, params, nil, nil, options...)
  888. if err != nil {
  889. return err
  890. }
  891. defer resp.Body.Close()
  892. return checkRespCode(resp.StatusCode, []int{http.StatusNoContent})
  893. }
  894. // SetBucketRequestPayment API operation for Object Storage Service.
  895. //
  896. // Set the requestPayment of bucket
  897. //
  898. // bucketName the bucket name.
  899. //
  900. // paymentConfig the payment configuration
  901. //
  902. // error it's nil if no error, otherwise it's an error object.
  903. //
  904. func (client Client) SetBucketRequestPayment(bucketName string, paymentConfig RequestPaymentConfiguration, options ...Option) error {
  905. params := map[string]interface{}{}
  906. params["requestPayment"] = nil
  907. var bs []byte
  908. bs, err := xml.Marshal(paymentConfig)
  909. if err != nil {
  910. return err
  911. }
  912. buffer := new(bytes.Buffer)
  913. buffer.Write(bs)
  914. contentType := http.DetectContentType(buffer.Bytes())
  915. headers := map[string]string{}
  916. headers[HTTPHeaderContentType] = contentType
  917. resp, err := client.do("PUT", bucketName, params, headers, buffer, options...)
  918. if err != nil {
  919. return err
  920. }
  921. defer resp.Body.Close()
  922. return checkRespCode(resp.StatusCode, []int{http.StatusOK})
  923. }
  924. // GetBucketRequestPayment API operation for Object Storage Service.
  925. //
  926. // Get bucket requestPayment
  927. //
  928. // bucketName the bucket name.
  929. //
  930. // RequestPaymentConfiguration the payment configuration
  931. //
  932. // error it's nil if no error, otherwise it's an error object.
  933. //
  934. func (client Client) GetBucketRequestPayment(bucketName string, options ...Option) (RequestPaymentConfiguration, error) {
  935. var out RequestPaymentConfiguration
  936. params := map[string]interface{}{}
  937. params["requestPayment"] = nil
  938. resp, err := client.do("GET", bucketName, params, nil, nil, options...)
  939. if err != nil {
  940. return out, err
  941. }
  942. defer resp.Body.Close()
  943. err = xmlUnmarshal(resp.Body, &out)
  944. return out, err
  945. }
  946. // GetUserQoSInfo API operation for Object Storage Service.
  947. //
  948. // Get user qos.
  949. //
  950. // UserQoSConfiguration the User Qos and range Information.
  951. //
  952. // error it's nil if no error, otherwise it's an error object.
  953. //
  954. func (client Client) GetUserQoSInfo(options ...Option) (UserQoSConfiguration, error) {
  955. var out UserQoSConfiguration
  956. params := map[string]interface{}{}
  957. params["qosInfo"] = nil
  958. resp, err := client.do("GET", "", params, nil, nil, options...)
  959. if err != nil {
  960. return out, err
  961. }
  962. defer resp.Body.Close()
  963. err = xmlUnmarshal(resp.Body, &out)
  964. return out, err
  965. }
  966. // SetBucketQoSInfo API operation for Object Storage Service.
  967. //
  968. // Set Bucket Qos information.
  969. //
  970. // bucketName tht bucket name.
  971. //
  972. // qosConf the qos configuration.
  973. //
  974. // error it's nil if no error, otherwise it's an error object.
  975. //
  976. func (client Client) SetBucketQoSInfo(bucketName string, qosConf BucketQoSConfiguration, options ...Option) error {
  977. params := map[string]interface{}{}
  978. params["qosInfo"] = nil
  979. var bs []byte
  980. bs, err := xml.Marshal(qosConf)
  981. if err != nil {
  982. return err
  983. }
  984. buffer := new(bytes.Buffer)
  985. buffer.Write(bs)
  986. contentTpye := http.DetectContentType(buffer.Bytes())
  987. headers := map[string]string{}
  988. headers[HTTPHeaderContentType] = contentTpye
  989. resp, err := client.do("PUT", bucketName, params, headers, buffer, options...)
  990. if err != nil {
  991. return err
  992. }
  993. defer resp.Body.Close()
  994. return checkRespCode(resp.StatusCode, []int{http.StatusOK})
  995. }
  996. // GetBucketQosInfo API operation for Object Storage Service.
  997. //
  998. // Get Bucket Qos information.
  999. //
  1000. // bucketName tht bucket name.
  1001. //
  1002. // BucketQoSConfiguration the return qos configuration.
  1003. //
  1004. // error it's nil if no error, otherwise it's an error object.
  1005. //
  1006. func (client Client) GetBucketQosInfo(bucketName string, options ...Option) (BucketQoSConfiguration, error) {
  1007. var out BucketQoSConfiguration
  1008. params := map[string]interface{}{}
  1009. params["qosInfo"] = nil
  1010. resp, err := client.do("GET", bucketName, params, nil, nil, options...)
  1011. if err != nil {
  1012. return out, err
  1013. }
  1014. defer resp.Body.Close()
  1015. err = xmlUnmarshal(resp.Body, &out)
  1016. return out, err
  1017. }
  1018. // DeleteBucketQosInfo API operation for Object Storage Service.
  1019. //
  1020. // Delete Bucket QoS information.
  1021. //
  1022. // bucketName tht bucket name.
  1023. //
  1024. // error it's nil if no error, otherwise it's an error object.
  1025. //
  1026. func (client Client) DeleteBucketQosInfo(bucketName string, options ...Option) error {
  1027. params := map[string]interface{}{}
  1028. params["qosInfo"] = nil
  1029. resp, err := client.do("DELETE", bucketName, params, nil, nil, options...)
  1030. if err != nil {
  1031. return err
  1032. }
  1033. defer resp.Body.Close()
  1034. return checkRespCode(resp.StatusCode, []int{http.StatusNoContent})
  1035. }
  1036. // LimitUploadSpeed set upload bandwidth limit speed,default is 0,unlimited
  1037. // upSpeed KB/s, 0 is unlimited,default is 0
  1038. // error it's nil if success, otherwise failure
  1039. func (client Client) LimitUploadSpeed(upSpeed int) error {
  1040. if client.Config == nil {
  1041. return fmt.Errorf("client config is nil")
  1042. }
  1043. return client.Config.LimitUploadSpeed(upSpeed)
  1044. }
  1045. // UseCname sets the flag of using CName. By default it's false.
  1046. //
  1047. // isUseCname true: the endpoint has the CName, false: the endpoint does not have cname. Default is false.
  1048. //
  1049. func UseCname(isUseCname bool) ClientOption {
  1050. return func(client *Client) {
  1051. client.Config.IsCname = isUseCname
  1052. client.Conn.url.Init(client.Config.Endpoint, client.Config.IsCname, client.Config.IsUseProxy)
  1053. }
  1054. }
  1055. // Timeout sets the HTTP timeout in seconds.
  1056. //
  1057. // connectTimeoutSec HTTP timeout in seconds. Default is 10 seconds. 0 means infinite (not recommended)
  1058. // readWriteTimeout HTTP read or write's timeout in seconds. Default is 20 seconds. 0 means infinite.
  1059. //
  1060. func Timeout(connectTimeoutSec, readWriteTimeout int64) ClientOption {
  1061. return func(client *Client) {
  1062. client.Config.HTTPTimeout.ConnectTimeout =
  1063. time.Second * time.Duration(connectTimeoutSec)
  1064. client.Config.HTTPTimeout.ReadWriteTimeout =
  1065. time.Second * time.Duration(readWriteTimeout)
  1066. client.Config.HTTPTimeout.HeaderTimeout =
  1067. time.Second * time.Duration(readWriteTimeout)
  1068. client.Config.HTTPTimeout.IdleConnTimeout =
  1069. time.Second * time.Duration(readWriteTimeout)
  1070. client.Config.HTTPTimeout.LongTimeout =
  1071. time.Second * time.Duration(readWriteTimeout*10)
  1072. }
  1073. }
  1074. // SecurityToken sets the temporary user's SecurityToken.
  1075. //
  1076. // token STS token
  1077. //
  1078. func SecurityToken(token string) ClientOption {
  1079. return func(client *Client) {
  1080. client.Config.SecurityToken = strings.TrimSpace(token)
  1081. }
  1082. }
  1083. // EnableMD5 enables MD5 validation.
  1084. //
  1085. // isEnableMD5 true: enable MD5 validation; false: disable MD5 validation.
  1086. //
  1087. func EnableMD5(isEnableMD5 bool) ClientOption {
  1088. return func(client *Client) {
  1089. client.Config.IsEnableMD5 = isEnableMD5
  1090. }
  1091. }
  1092. // MD5ThresholdCalcInMemory sets the memory usage threshold for computing the MD5, default is 16MB.
  1093. //
  1094. // threshold the memory threshold in bytes. When the uploaded content is more than 16MB, the temp file is used for computing the MD5.
  1095. //
  1096. func MD5ThresholdCalcInMemory(threshold int64) ClientOption {
  1097. return func(client *Client) {
  1098. client.Config.MD5Threshold = threshold
  1099. }
  1100. }
  1101. // EnableCRC enables the CRC checksum. Default is true.
  1102. //
  1103. // isEnableCRC true: enable CRC checksum; false: disable the CRC checksum.
  1104. //
  1105. func EnableCRC(isEnableCRC bool) ClientOption {
  1106. return func(client *Client) {
  1107. client.Config.IsEnableCRC = isEnableCRC
  1108. }
  1109. }
  1110. // UserAgent specifies UserAgent. The default is aliyun-sdk-go/1.2.0 (windows/-/amd64;go1.5.2).
  1111. //
  1112. // userAgent the user agent string.
  1113. //
  1114. func UserAgent(userAgent string) ClientOption {
  1115. return func(client *Client) {
  1116. client.Config.UserAgent = userAgent
  1117. }
  1118. }
  1119. // Proxy sets the proxy (optional). The default is not using proxy.
  1120. //
  1121. // proxyHost the proxy host in the format "host:port". For example, proxy.com:80 .
  1122. //
  1123. func Proxy(proxyHost string) ClientOption {
  1124. return func(client *Client) {
  1125. client.Config.IsUseProxy = true
  1126. client.Config.ProxyHost = proxyHost
  1127. client.Conn.url.Init(client.Config.Endpoint, client.Config.IsCname, client.Config.IsUseProxy)
  1128. }
  1129. }
  1130. // AuthProxy sets the proxy information with user name and password.
  1131. //
  1132. // proxyHost the proxy host in the format "host:port". For example, proxy.com:80 .
  1133. // proxyUser the proxy user name.
  1134. // proxyPassword the proxy password.
  1135. //
  1136. func AuthProxy(proxyHost, proxyUser, proxyPassword string) ClientOption {
  1137. return func(client *Client) {
  1138. client.Config.IsUseProxy = true
  1139. client.Config.ProxyHost = proxyHost
  1140. client.Config.IsAuthProxy = true
  1141. client.Config.ProxyUser = proxyUser
  1142. client.Config.ProxyPassword = proxyPassword
  1143. client.Conn.url.Init(client.Config.Endpoint, client.Config.IsCname, client.Config.IsUseProxy)
  1144. }
  1145. }
  1146. //
  1147. // HTTPClient sets the http.Client in use to the one passed in
  1148. //
  1149. func HTTPClient(HTTPClient *http.Client) ClientOption {
  1150. return func(client *Client) {
  1151. client.HTTPClient = HTTPClient
  1152. }
  1153. }
  1154. //
  1155. // SetLogLevel sets the oss sdk log level
  1156. //
  1157. func SetLogLevel(LogLevel int) ClientOption {
  1158. return func(client *Client) {
  1159. client.Config.LogLevel = LogLevel
  1160. }
  1161. }
  1162. //
  1163. // SetLogger sets the oss sdk logger
  1164. //
  1165. func SetLogger(Logger *log.Logger) ClientOption {
  1166. return func(client *Client) {
  1167. client.Config.Logger = Logger
  1168. }
  1169. }
  1170. // SetCredentialsProvider sets funciton for get the user's ak
  1171. func SetCredentialsProvider(provider CredentialsProvider) ClientOption {
  1172. return func(client *Client) {
  1173. client.Config.CredentialsProvider = provider
  1174. }
  1175. }
  1176. // SetLocalAddr sets funciton for local addr
  1177. func SetLocalAddr(localAddr net.Addr) ClientOption {
  1178. return func(client *Client) {
  1179. client.Config.LocalAddr = localAddr
  1180. }
  1181. }
  1182. // Private
  1183. func (client Client) do(method, bucketName string, params map[string]interface{},
  1184. headers map[string]string, data io.Reader, options ...Option) (*Response, error) {
  1185. err := CheckBucketName(bucketName)
  1186. if len(bucketName) > 0 && err != nil {
  1187. return nil, err
  1188. }
  1189. resp, err := client.Conn.Do(method, bucketName, "", params, headers, data, 0, nil)
  1190. // get response header
  1191. respHeader, _ := findOption(options, responseHeader, nil)
  1192. if respHeader != nil {
  1193. pRespHeader := respHeader.(*http.Header)
  1194. *pRespHeader = resp.Headers
  1195. }
  1196. return resp, err
  1197. }