bucket.go 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  1. package oss
  2. import (
  3. "bytes"
  4. "crypto/md5"
  5. "encoding/base64"
  6. "encoding/xml"
  7. "fmt"
  8. "hash"
  9. "hash/crc64"
  10. "io"
  11. "net/http"
  12. "net/url"
  13. "os"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. // Bucket implements the operations of object.
  19. type Bucket struct {
  20. Client Client
  21. BucketName string
  22. }
  23. // PutObject creates a new object and it will overwrite the original one if it exists already.
  24. //
  25. // objectKey the object key in UTF-8 encoding. The length must be between 1 and 1023, and cannot start with "/" or "\".
  26. // reader io.Reader instance for reading the data for uploading
  27. // options the options for uploading the object. The valid options here are CacheControl, ContentDisposition, ContentEncoding
  28. // Expires, ServerSideEncryption, ObjectACL and Meta. Refer to the link below for more details.
  29. // https://help.aliyun.com/document_detail/oss/api-reference/object/PutObject.html
  30. //
  31. // error it's nil if no error, otherwise it's an error object.
  32. //
  33. func (bucket Bucket) PutObject(objectKey string, reader io.Reader, options ...Option) error {
  34. opts := AddContentType(options, objectKey)
  35. request := &PutObjectRequest{
  36. ObjectKey: objectKey,
  37. Reader: reader,
  38. }
  39. resp, err := bucket.DoPutObject(request, opts)
  40. if err != nil {
  41. return err
  42. }
  43. defer resp.Body.Close()
  44. return err
  45. }
  46. // PutObjectFromFile creates a new object from the local file.
  47. //
  48. // objectKey object key.
  49. // filePath the local file path to upload.
  50. // options the options for uploading the object. Refer to the parameter options in PutObject for more details.
  51. //
  52. // error it's nil if no error, otherwise it's an error object.
  53. //
  54. func (bucket Bucket) PutObjectFromFile(objectKey, filePath string, options ...Option) error {
  55. fd, err := os.Open(filePath)
  56. if err != nil {
  57. return err
  58. }
  59. defer fd.Close()
  60. opts := AddContentType(options, filePath, objectKey)
  61. request := &PutObjectRequest{
  62. ObjectKey: objectKey,
  63. Reader: fd,
  64. }
  65. resp, err := bucket.DoPutObject(request, opts)
  66. if err != nil {
  67. return err
  68. }
  69. defer resp.Body.Close()
  70. return err
  71. }
  72. // DoPutObject does the actual upload work.
  73. //
  74. // request the request instance for uploading an object.
  75. // options the options for uploading an object.
  76. //
  77. // Response the response from OSS.
  78. // error it's nil if no error, otherwise it's an error object.
  79. //
  80. func (bucket Bucket) DoPutObject(request *PutObjectRequest, options []Option) (*Response, error) {
  81. isOptSet, _, _ := IsOptionSet(options, HTTPHeaderContentType)
  82. if !isOptSet {
  83. options = AddContentType(options, request.ObjectKey)
  84. }
  85. listener := GetProgressListener(options)
  86. params := map[string]interface{}{}
  87. resp, err := bucket.do("PUT", request.ObjectKey, params, options, request.Reader, listener)
  88. if err != nil {
  89. return nil, err
  90. }
  91. if bucket.GetConfig().IsEnableCRC {
  92. err = CheckCRC(resp, "DoPutObject")
  93. if err != nil {
  94. return resp, err
  95. }
  96. }
  97. err = CheckRespCode(resp.StatusCode, []int{http.StatusOK})
  98. return resp, err
  99. }
  100. // GetObject downloads the object.
  101. //
  102. // objectKey the object key.
  103. // options the options for downloading the object. The valid values are: Range, IfModifiedSince, IfUnmodifiedSince, IfMatch,
  104. // IfNoneMatch, AcceptEncoding. For more details, please check out:
  105. // https://help.aliyun.com/document_detail/oss/api-reference/object/GetObject.html
  106. //
  107. // io.ReadCloser reader instance for reading data from response. It must be called close() after the usage and only valid when error is nil.
  108. // error it's nil if no error, otherwise it's an error object.
  109. //
  110. func (bucket Bucket) GetObject(objectKey string, options ...Option) (io.ReadCloser, error) {
  111. result, err := bucket.DoGetObject(&GetObjectRequest{objectKey}, options)
  112. if err != nil {
  113. return nil, err
  114. }
  115. return result.Response, nil
  116. }
  117. // GetObjectToFile downloads the data to a local file.
  118. //
  119. // objectKey the object key to download.
  120. // filePath the local file to store the object data.
  121. // options the options for downloading the object. Refer to the parameter options in method GetObject for more details.
  122. //
  123. // error it's nil if no error, otherwise it's an error object.
  124. //
  125. func (bucket Bucket) GetObjectToFile(objectKey, filePath string, options ...Option) error {
  126. tempFilePath := filePath + TempFileSuffix
  127. // Calls the API to actually download the object. Returns the result instance.
  128. result, err := bucket.DoGetObject(&GetObjectRequest{objectKey}, options)
  129. if err != nil {
  130. return err
  131. }
  132. defer result.Response.Close()
  133. // If the local file does not exist, create a new one. If it exists, overwrite it.
  134. fd, err := os.OpenFile(tempFilePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, FilePermMode)
  135. if err != nil {
  136. return err
  137. }
  138. // Copy the data to the local file path.
  139. _, err = io.Copy(fd, result.Response.Body)
  140. fd.Close()
  141. if err != nil {
  142. return err
  143. }
  144. // Compares the CRC value
  145. hasRange, _, _ := IsOptionSet(options, HTTPHeaderRange)
  146. encodeOpt, _ := FindOption(options, HTTPHeaderAcceptEncoding, nil)
  147. acceptEncoding := ""
  148. if encodeOpt != nil {
  149. acceptEncoding = encodeOpt.(string)
  150. }
  151. if bucket.GetConfig().IsEnableCRC && !hasRange && acceptEncoding != "gzip" {
  152. result.Response.ClientCRC = result.ClientCRC.Sum64()
  153. err = CheckCRC(result.Response, "GetObjectToFile")
  154. if err != nil {
  155. os.Remove(tempFilePath)
  156. return err
  157. }
  158. }
  159. return os.Rename(tempFilePath, filePath)
  160. }
  161. // DoGetObject is the actual API that gets the object. It's the internal function called by other public APIs.
  162. //
  163. // request the request to download the object.
  164. // options the options for downloading the file. Checks out the parameter options in method GetObject.
  165. //
  166. // GetObjectResult the result instance of getting the object.
  167. // error it's nil if no error, otherwise it's an error object.
  168. //
  169. func (bucket Bucket) DoGetObject(request *GetObjectRequest, options []Option) (*GetObjectResult, error) {
  170. params, _ := GetRawParams(options)
  171. resp, err := bucket.do("GET", request.ObjectKey, params, options, nil, nil)
  172. if err != nil {
  173. return nil, err
  174. }
  175. result := &GetObjectResult{
  176. Response: resp,
  177. }
  178. // CRC
  179. var crcCalc hash.Hash64
  180. hasRange, _, _ := IsOptionSet(options, HTTPHeaderRange)
  181. if bucket.GetConfig().IsEnableCRC && !hasRange {
  182. crcCalc = crc64.New(CrcTable())
  183. result.ServerCRC = resp.ServerCRC
  184. result.ClientCRC = crcCalc
  185. }
  186. // Progress
  187. listener := GetProgressListener(options)
  188. contentLen, _ := strconv.ParseInt(resp.Headers.Get(HTTPHeaderContentLength), 10, 64)
  189. resp.Body = TeeReader(resp.Body, crcCalc, contentLen, listener, nil)
  190. return result, nil
  191. }
  192. // CopyObject copies the object inside the bucket.
  193. //
  194. // srcObjectKey the source object to copy.
  195. // destObjectKey the target object to copy.
  196. // options options for copying an object. You can specify the conditions of copy. The valid conditions are CopySourceIfMatch,
  197. // CopySourceIfNoneMatch, CopySourceIfModifiedSince, CopySourceIfUnmodifiedSince, MetadataDirective.
  198. // Also you can specify the target object's attributes, such as CacheControl, ContentDisposition, ContentEncoding, Expires,
  199. // ServerSideEncryption, ObjectACL, Meta. Refer to the link below for more details :
  200. // https://help.aliyun.com/document_detail/oss/api-reference/object/CopyObject.html
  201. //
  202. // error it's nil if no error, otherwise it's an error object.
  203. //
  204. func (bucket Bucket) CopyObject(srcObjectKey, destObjectKey string, options ...Option) (CopyObjectResult, error) {
  205. var out CopyObjectResult
  206. //first find version id
  207. versionIdKey := "versionId"
  208. versionId, _ := FindOption(options, versionIdKey, nil)
  209. if versionId == nil {
  210. options = append(options, CopySource(bucket.BucketName, url.QueryEscape(srcObjectKey)))
  211. } else {
  212. options = DeleteOption(options, versionIdKey)
  213. options = append(options, CopySourceVersion(bucket.BucketName, url.QueryEscape(srcObjectKey), versionId.(string)))
  214. }
  215. params := map[string]interface{}{}
  216. resp, err := bucket.do("PUT", destObjectKey, params, options, nil, nil)
  217. if err != nil {
  218. return out, err
  219. }
  220. defer resp.Body.Close()
  221. err = xmlUnmarshal(resp.Body, &out)
  222. return out, err
  223. }
  224. // CopyObjectTo copies the object to another bucket.
  225. //
  226. // srcObjectKey source object key. The source bucket is Bucket.BucketName .
  227. // destBucketName target bucket name.
  228. // destObjectKey target object name.
  229. // options copy options, check out parameter options in function CopyObject for more details.
  230. //
  231. // error it's nil if no error, otherwise it's an error object.
  232. //
  233. func (bucket Bucket) CopyObjectTo(destBucketName, destObjectKey, srcObjectKey string, options ...Option) (CopyObjectResult, error) {
  234. return bucket.copy(srcObjectKey, destBucketName, destObjectKey, options...)
  235. }
  236. //
  237. // CopyObjectFrom copies the object to another bucket.
  238. //
  239. // srcBucketName source bucket name.
  240. // srcObjectKey source object name.
  241. // destObjectKey target object name. The target bucket name is Bucket.BucketName.
  242. // options copy options. Check out parameter options in function CopyObject.
  243. //
  244. // error it's nil if no error, otherwise it's an error object.
  245. //
  246. func (bucket Bucket) CopyObjectFrom(srcBucketName, srcObjectKey, destObjectKey string, options ...Option) (CopyObjectResult, error) {
  247. destBucketName := bucket.BucketName
  248. var out CopyObjectResult
  249. srcBucket, err := bucket.Client.Bucket(srcBucketName)
  250. if err != nil {
  251. return out, err
  252. }
  253. return srcBucket.copy(srcObjectKey, destBucketName, destObjectKey, options...)
  254. }
  255. func (bucket Bucket) copy(srcObjectKey, destBucketName, destObjectKey string, options ...Option) (CopyObjectResult, error) {
  256. var out CopyObjectResult
  257. //first find version id
  258. versionIdKey := "versionId"
  259. versionId, _ := FindOption(options, versionIdKey, nil)
  260. if versionId == nil {
  261. options = append(options, CopySource(bucket.BucketName, url.QueryEscape(srcObjectKey)))
  262. } else {
  263. options = DeleteOption(options, versionIdKey)
  264. options = append(options, CopySourceVersion(bucket.BucketName, url.QueryEscape(srcObjectKey), versionId.(string)))
  265. }
  266. headers := make(map[string]string)
  267. err := handleOptions(headers, options)
  268. if err != nil {
  269. return out, err
  270. }
  271. params := map[string]interface{}{}
  272. resp, err := bucket.Client.Conn.Do("PUT", destBucketName, destObjectKey, params, headers, nil, 0, nil)
  273. // get response header
  274. respHeader, _ := FindOption(options, responseHeader, nil)
  275. if respHeader != nil {
  276. pRespHeader := respHeader.(*http.Header)
  277. *pRespHeader = resp.Headers
  278. }
  279. if err != nil {
  280. return out, err
  281. }
  282. defer resp.Body.Close()
  283. err = xmlUnmarshal(resp.Body, &out)
  284. return out, err
  285. }
  286. // AppendObject uploads the data in the way of appending an existing or new object.
  287. //
  288. // AppendObject the parameter appendPosition specifies which postion (in the target object) to append. For the first append (to a non-existing file),
  289. // the appendPosition should be 0. The appendPosition in the subsequent calls will be the current object length.
  290. // For example, the first appendObject's appendPosition is 0 and it uploaded 65536 bytes data, then the second call's position is 65536.
  291. // The response header x-oss-next-append-position after each successful request also specifies the next call's append position (so the caller need not to maintain this information).
  292. //
  293. // objectKey the target object to append to.
  294. // reader io.Reader. The read instance for reading the data to append.
  295. // appendPosition the start position to append.
  296. // destObjectProperties the options for the first appending, such as CacheControl, ContentDisposition, ContentEncoding,
  297. // Expires, ServerSideEncryption, ObjectACL.
  298. //
  299. // int64 the next append position, it's valid when error is nil.
  300. // error it's nil if no error, otherwise it's an error object.
  301. //
  302. func (bucket Bucket) AppendObject(objectKey string, reader io.Reader, appendPosition int64, options ...Option) (int64, error) {
  303. request := &AppendObjectRequest{
  304. ObjectKey: objectKey,
  305. Reader: reader,
  306. Position: appendPosition,
  307. }
  308. result, err := bucket.DoAppendObject(request, options)
  309. if err != nil {
  310. return appendPosition, err
  311. }
  312. return result.NextPosition, err
  313. }
  314. // DoAppendObject is the actual API that does the object append.
  315. //
  316. // request the request object for appending object.
  317. // options the options for appending object.
  318. //
  319. // AppendObjectResult the result object for appending object.
  320. // error it's nil if no error, otherwise it's an error object.
  321. //
  322. func (bucket Bucket) DoAppendObject(request *AppendObjectRequest, options []Option) (*AppendObjectResult, error) {
  323. params := map[string]interface{}{}
  324. params["append"] = nil
  325. params["position"] = strconv.FormatInt(request.Position, 10)
  326. headers := make(map[string]string)
  327. opts := AddContentType(options, request.ObjectKey)
  328. handleOptions(headers, opts)
  329. var initCRC uint64
  330. isCRCSet, initCRCOpt, _ := IsOptionSet(options, initCRC64)
  331. if isCRCSet {
  332. initCRC = initCRCOpt.(uint64)
  333. }
  334. listener := GetProgressListener(options)
  335. handleOptions(headers, opts)
  336. resp, err := bucket.Client.Conn.Do("POST", bucket.BucketName, request.ObjectKey, params, headers,
  337. request.Reader, initCRC, listener)
  338. // get response header
  339. respHeader, _ := FindOption(options, responseHeader, nil)
  340. if respHeader != nil {
  341. pRespHeader := respHeader.(*http.Header)
  342. *pRespHeader = resp.Headers
  343. }
  344. if err != nil {
  345. return nil, err
  346. }
  347. defer resp.Body.Close()
  348. nextPosition, _ := strconv.ParseInt(resp.Headers.Get(HTTPHeaderOssNextAppendPosition), 10, 64)
  349. result := &AppendObjectResult{
  350. NextPosition: nextPosition,
  351. CRC: resp.ServerCRC,
  352. }
  353. if bucket.GetConfig().IsEnableCRC && isCRCSet {
  354. err = CheckCRC(resp, "AppendObject")
  355. if err != nil {
  356. return result, err
  357. }
  358. }
  359. return result, nil
  360. }
  361. // DeleteObject deletes the object.
  362. //
  363. // objectKey the object key to delete.
  364. //
  365. // error it's nil if no error, otherwise it's an error object.
  366. //
  367. func (bucket Bucket) DeleteObject(objectKey string, options ...Option) error {
  368. params, _ := GetRawParams(options)
  369. resp, err := bucket.do("DELETE", objectKey, params, options, nil, nil)
  370. if err != nil {
  371. return err
  372. }
  373. defer resp.Body.Close()
  374. return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent})
  375. }
  376. // DeleteObjects deletes multiple objects.
  377. //
  378. // objectKeys the object keys to delete.
  379. // options the options for deleting objects.
  380. // Supported option is DeleteObjectsQuiet which means it will not return error even deletion failed (not recommended). By default it's not used.
  381. //
  382. // DeleteObjectsResult the result object.
  383. // error it's nil if no error, otherwise it's an error object.
  384. //
  385. func (bucket Bucket) DeleteObjects(objectKeys []string, options ...Option) (DeleteObjectsResult, error) {
  386. out := DeleteObjectsResult{}
  387. dxml := deleteXML{}
  388. for _, key := range objectKeys {
  389. dxml.Objects = append(dxml.Objects, DeleteObject{Key: key})
  390. }
  391. isQuiet, _ := FindOption(options, deleteObjectsQuiet, false)
  392. dxml.Quiet = isQuiet.(bool)
  393. bs, err := xml.Marshal(dxml)
  394. if err != nil {
  395. return out, err
  396. }
  397. buffer := new(bytes.Buffer)
  398. buffer.Write(bs)
  399. contentType := http.DetectContentType(buffer.Bytes())
  400. options = append(options, ContentType(contentType))
  401. sum := md5.Sum(bs)
  402. b64 := base64.StdEncoding.EncodeToString(sum[:])
  403. options = append(options, ContentMD5(b64))
  404. params := map[string]interface{}{}
  405. params["delete"] = nil
  406. params["encoding-type"] = "url"
  407. resp, err := bucket.do("POST", "", params, options, buffer, nil)
  408. if err != nil {
  409. return out, err
  410. }
  411. defer resp.Body.Close()
  412. deletedResult := DeleteObjectVersionsResult{}
  413. if !dxml.Quiet {
  414. if err = xmlUnmarshal(resp.Body, &deletedResult); err == nil {
  415. err = decodeDeleteObjectsResult(&deletedResult)
  416. }
  417. }
  418. // Keep compatibility:need convert to struct DeleteObjectsResult
  419. out.XMLName = deletedResult.XMLName
  420. for _, v := range deletedResult.DeletedObjectsDetail {
  421. out.DeletedObjects = append(out.DeletedObjects, v.Key)
  422. }
  423. return out, err
  424. }
  425. // DeleteObjectVersions deletes multiple object versions.
  426. //
  427. // objectVersions the object keys and versions to delete.
  428. // options the options for deleting objects.
  429. // Supported option is DeleteObjectsQuiet which means it will not return error even deletion failed (not recommended). By default it's not used.
  430. //
  431. // DeleteObjectVersionsResult the result object.
  432. // error it's nil if no error, otherwise it's an error object.
  433. //
  434. func (bucket Bucket) DeleteObjectVersions(objectVersions []DeleteObject, options ...Option) (DeleteObjectVersionsResult, error) {
  435. out := DeleteObjectVersionsResult{}
  436. dxml := deleteXML{}
  437. dxml.Objects = objectVersions
  438. isQuiet, _ := FindOption(options, deleteObjectsQuiet, false)
  439. dxml.Quiet = isQuiet.(bool)
  440. bs, err := xml.Marshal(dxml)
  441. if err != nil {
  442. return out, err
  443. }
  444. buffer := new(bytes.Buffer)
  445. buffer.Write(bs)
  446. contentType := http.DetectContentType(buffer.Bytes())
  447. options = append(options, ContentType(contentType))
  448. sum := md5.Sum(bs)
  449. b64 := base64.StdEncoding.EncodeToString(sum[:])
  450. options = append(options, ContentMD5(b64))
  451. params := map[string]interface{}{}
  452. params["delete"] = nil
  453. params["encoding-type"] = "url"
  454. resp, err := bucket.do("POST", "", params, options, buffer, nil)
  455. if err != nil {
  456. return out, err
  457. }
  458. defer resp.Body.Close()
  459. if !dxml.Quiet {
  460. if err = xmlUnmarshal(resp.Body, &out); err == nil {
  461. err = decodeDeleteObjectsResult(&out)
  462. }
  463. }
  464. return out, err
  465. }
  466. // IsObjectExist checks if the object exists.
  467. //
  468. // bool flag of object's existence (true:exists; false:non-exist) when error is nil.
  469. //
  470. // error it's nil if no error, otherwise it's an error object.
  471. //
  472. func (bucket Bucket) IsObjectExist(objectKey string, options ...Option) (bool, error) {
  473. _, err := bucket.GetObjectMeta(objectKey, options...)
  474. if err == nil {
  475. return true, nil
  476. }
  477. switch err.(type) {
  478. case ServiceError:
  479. if err.(ServiceError).StatusCode == 404 {
  480. return false, nil
  481. }
  482. }
  483. return false, err
  484. }
  485. // ListObjects lists the objects under the current bucket.
  486. //
  487. // options it contains all the filters for listing objects.
  488. // It could specify a prefix filter on object keys, the max keys count to return and the object key marker and the delimiter for grouping object names.
  489. // The key marker means the returned objects' key must be greater than it in lexicographic order.
  490. //
  491. // For example, if the bucket has 8 objects, my-object-1, my-object-11, my-object-2, my-object-21,
  492. // my-object-22, my-object-3, my-object-31, my-object-32. If the prefix is my-object-2 (no other filters), then it returns
  493. // my-object-2, my-object-21, my-object-22 three objects. If the marker is my-object-22 (no other filters), then it returns
  494. // my-object-3, my-object-31, my-object-32 three objects. If the max keys is 5, then it returns 5 objects.
  495. // The three filters could be used together to achieve filter and paging functionality.
  496. // If the prefix is the folder name, then it could list all files under this folder (including the files under its subfolders).
  497. // But if the delimiter is specified with '/', then it only returns that folder's files (no subfolder's files). The direct subfolders are in the commonPrefixes properties.
  498. // For example, if the bucket has three objects fun/test.jpg, fun/movie/001.avi, fun/movie/007.avi. And if the prefix is "fun/", then it returns all three objects.
  499. // But if the delimiter is '/', then only "fun/test.jpg" is returned as files and fun/movie/ is returned as common prefix.
  500. //
  501. // For common usage scenario, check out sample/list_object.go.
  502. //
  503. // ListObjectsResponse the return value after operation succeeds (only valid when error is nil).
  504. //
  505. func (bucket Bucket) ListObjects(options ...Option) (ListObjectsResult, error) {
  506. var out ListObjectsResult
  507. options = append(options, EncodingType("url"))
  508. params, err := GetRawParams(options)
  509. if err != nil {
  510. return out, err
  511. }
  512. resp, err := bucket.do("GET", "", params, options, nil, nil)
  513. if err != nil {
  514. return out, err
  515. }
  516. defer resp.Body.Close()
  517. err = xmlUnmarshal(resp.Body, &out)
  518. if err != nil {
  519. return out, err
  520. }
  521. err = decodeListObjectsResult(&out)
  522. return out, err
  523. }
  524. // ListObjectVersions lists objects of all versions under the current bucket.
  525. func (bucket Bucket) ListObjectVersions(options ...Option) (ListObjectVersionsResult, error) {
  526. var out ListObjectVersionsResult
  527. options = append(options, EncodingType("url"))
  528. params, err := GetRawParams(options)
  529. if err != nil {
  530. return out, err
  531. }
  532. params["versions"] = nil
  533. resp, err := bucket.do("GET", "", params, options, nil, nil)
  534. if err != nil {
  535. return out, err
  536. }
  537. defer resp.Body.Close()
  538. err = xmlUnmarshal(resp.Body, &out)
  539. if err != nil {
  540. return out, err
  541. }
  542. err = decodeListObjectVersionsResult(&out)
  543. return out, err
  544. }
  545. // SetObjectMeta sets the metadata of the Object.
  546. //
  547. // objectKey object
  548. // options options for setting the metadata. The valid options are CacheControl, ContentDisposition, ContentEncoding, Expires,
  549. // ServerSideEncryption, and custom metadata.
  550. //
  551. // error it's nil if no error, otherwise it's an error object.
  552. //
  553. func (bucket Bucket) SetObjectMeta(objectKey string, options ...Option) error {
  554. options = append(options, MetadataDirective(MetaReplace))
  555. _, err := bucket.CopyObject(objectKey, objectKey, options...)
  556. return err
  557. }
  558. // GetObjectDetailedMeta gets the object's detailed metadata
  559. //
  560. // objectKey object key.
  561. // options the constraints of the object. Only when the object meets the requirements this method will return the metadata. Otherwise returns error. Valid options are IfModifiedSince, IfUnmodifiedSince,
  562. // IfMatch, IfNoneMatch. For more details check out https://help.aliyun.com/document_detail/oss/api-reference/object/HeadObject.html
  563. //
  564. // http.Header object meta when error is nil.
  565. // error it's nil if no error, otherwise it's an error object.
  566. //
  567. func (bucket Bucket) GetObjectDetailedMeta(objectKey string, options ...Option) (http.Header, error) {
  568. params, _ := GetRawParams(options)
  569. resp, err := bucket.do("HEAD", objectKey, params, options, nil, nil)
  570. if err != nil {
  571. return nil, err
  572. }
  573. defer resp.Body.Close()
  574. return resp.Headers, nil
  575. }
  576. // GetObjectMeta gets object metadata.
  577. //
  578. // GetObjectMeta is more lightweight than GetObjectDetailedMeta as it only returns basic metadata including ETag
  579. // size, LastModified. The size information is in the HTTP header Content-Length.
  580. //
  581. // objectKey object key
  582. //
  583. // http.Header the object's metadata, valid when error is nil.
  584. // error it's nil if no error, otherwise it's an error object.
  585. //
  586. func (bucket Bucket) GetObjectMeta(objectKey string, options ...Option) (http.Header, error) {
  587. params, _ := GetRawParams(options)
  588. params["objectMeta"] = nil
  589. //resp, err := bucket.do("GET", objectKey, "?objectMeta", "", nil, nil, nil)
  590. resp, err := bucket.do("HEAD", objectKey, params, options, nil, nil)
  591. if err != nil {
  592. return nil, err
  593. }
  594. defer resp.Body.Close()
  595. return resp.Headers, nil
  596. }
  597. // SetObjectACL updates the object's ACL.
  598. //
  599. // Only the bucket's owner could update object's ACL which priority is higher than bucket's ACL.
  600. // For example, if the bucket ACL is private and object's ACL is public-read-write.
  601. // Then object's ACL is used and it means all users could read or write that object.
  602. // When the object's ACL is not set, then bucket's ACL is used as the object's ACL.
  603. //
  604. // Object read operations include GetObject, HeadObject, CopyObject and UploadPartCopy on the source object;
  605. // Object write operations include PutObject, PostObject, AppendObject, DeleteObject, DeleteMultipleObjects,
  606. // CompleteMultipartUpload and CopyObject on target object.
  607. //
  608. // objectKey the target object key (to set the ACL on)
  609. // objectAcl object ACL. Valid options are PrivateACL, PublicReadACL, PublicReadWriteACL.
  610. //
  611. // error it's nil if no error, otherwise it's an error object.
  612. //
  613. func (bucket Bucket) SetObjectACL(objectKey string, objectACL ACLType, options ...Option) error {
  614. options = append(options, ObjectACL(objectACL))
  615. params, _ := GetRawParams(options)
  616. params["acl"] = nil
  617. resp, err := bucket.do("PUT", objectKey, params, options, nil, nil)
  618. if err != nil {
  619. return err
  620. }
  621. defer resp.Body.Close()
  622. return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
  623. }
  624. // GetObjectACL gets object's ACL
  625. //
  626. // objectKey the object to get ACL from.
  627. //
  628. // GetObjectACLResult the result object when error is nil. GetObjectACLResult.Acl is the object ACL.
  629. // error it's nil if no error, otherwise it's an error object.
  630. //
  631. func (bucket Bucket) GetObjectACL(objectKey string, options ...Option) (GetObjectACLResult, error) {
  632. var out GetObjectACLResult
  633. params, _ := GetRawParams(options)
  634. params["acl"] = nil
  635. resp, err := bucket.do("GET", objectKey, params, options, 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. // PutSymlink creates a symlink (to point to an existing object)
  644. //
  645. // Symlink cannot point to another symlink.
  646. // When creating a symlink, it does not check the existence of the target file, and does not check if the target file is symlink.
  647. // Neither it checks the caller's permission on the target file. All these checks are deferred to the actual GetObject call via this symlink.
  648. // If trying to add an existing file, as long as the caller has the write permission, the existing one will be overwritten.
  649. // If the x-oss-meta- is specified, it will be added as the metadata of the symlink file.
  650. //
  651. // symObjectKey the symlink object's key.
  652. // targetObjectKey the target object key to point to.
  653. //
  654. // error it's nil if no error, otherwise it's an error object.
  655. //
  656. func (bucket Bucket) PutSymlink(symObjectKey string, targetObjectKey string, options ...Option) error {
  657. options = append(options, symlinkTarget(url.QueryEscape(targetObjectKey)))
  658. params, _ := GetRawParams(options)
  659. params["symlink"] = nil
  660. resp, err := bucket.do("PUT", symObjectKey, params, options, nil, nil)
  661. if err != nil {
  662. return err
  663. }
  664. defer resp.Body.Close()
  665. return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
  666. }
  667. // GetSymlink gets the symlink object with the specified key.
  668. // If the symlink object does not exist, returns 404.
  669. //
  670. // objectKey the symlink object's key.
  671. //
  672. // error it's nil if no error, otherwise it's an error object.
  673. // When error is nil, the target file key is in the X-Oss-Symlink-Target header of the returned object.
  674. //
  675. func (bucket Bucket) GetSymlink(objectKey string, options ...Option) (http.Header, error) {
  676. params, _ := GetRawParams(options)
  677. params["symlink"] = nil
  678. resp, err := bucket.do("GET", objectKey, params, options, nil, nil)
  679. if err != nil {
  680. return nil, err
  681. }
  682. defer resp.Body.Close()
  683. targetObjectKey := resp.Headers.Get(HTTPHeaderOssSymlinkTarget)
  684. targetObjectKey, err = url.QueryUnescape(targetObjectKey)
  685. if err != nil {
  686. return resp.Headers, err
  687. }
  688. resp.Headers.Set(HTTPHeaderOssSymlinkTarget, targetObjectKey)
  689. return resp.Headers, err
  690. }
  691. // RestoreObject restores the object from the archive storage.
  692. //
  693. // An archive object is in cold status by default and it cannot be accessed.
  694. // When restore is called on the cold object, it will become available for access after some time.
  695. // If multiple restores are called on the same file when the object is being restored, server side does nothing for additional calls but returns success.
  696. // By default, the restored object is available for access for one day. After that it will be unavailable again.
  697. // But if another RestoreObject are called after the file is restored, then it will extend one day's access time of that object, up to 7 days.
  698. //
  699. // objectKey object key to restore.
  700. //
  701. // error it's nil if no error, otherwise it's an error object.
  702. //
  703. func (bucket Bucket) RestoreObject(objectKey string, options ...Option) error {
  704. params, _ := GetRawParams(options)
  705. params["restore"] = nil
  706. resp, err := bucket.do("POST", objectKey, params, options, nil, nil)
  707. if err != nil {
  708. return err
  709. }
  710. defer resp.Body.Close()
  711. return CheckRespCode(resp.StatusCode, []int{http.StatusOK, http.StatusAccepted})
  712. }
  713. // RestoreObjectDetail support more features than RestoreObject
  714. func (bucket Bucket) RestoreObjectDetail(objectKey string, restoreConfig RestoreConfiguration, options ...Option) error {
  715. if restoreConfig.Tier == "" {
  716. // Expedited, Standard, Bulk
  717. restoreConfig.Tier = string(RestoreStandard)
  718. }
  719. if restoreConfig.Days == 0 {
  720. restoreConfig.Days = 1
  721. }
  722. bs, err := xml.Marshal(restoreConfig)
  723. if err != nil {
  724. return err
  725. }
  726. buffer := new(bytes.Buffer)
  727. buffer.Write(bs)
  728. contentType := http.DetectContentType(buffer.Bytes())
  729. options = append(options, ContentType(contentType))
  730. params, _ := GetRawParams(options)
  731. params["restore"] = nil
  732. resp, err := bucket.do("POST", objectKey, params, options, buffer, nil)
  733. if err != nil {
  734. return err
  735. }
  736. defer resp.Body.Close()
  737. return CheckRespCode(resp.StatusCode, []int{http.StatusOK, http.StatusAccepted})
  738. }
  739. // SignURL signs the URL. Users could access the object directly with this URL without getting the AK.
  740. //
  741. // objectKey the target object to sign.
  742. // signURLConfig the configuration for the signed URL
  743. //
  744. // string returns the signed URL, when error is nil.
  745. // error it's nil if no error, otherwise it's an error object.
  746. //
  747. func (bucket Bucket) SignURL(objectKey string, method HTTPMethod, expiredInSec int64, options ...Option) (string, error) {
  748. if expiredInSec < 0 {
  749. return "", fmt.Errorf("invalid expires: %d, expires must bigger than 0", expiredInSec)
  750. }
  751. expiration := time.Now().Unix() + expiredInSec
  752. params, err := GetRawParams(options)
  753. if err != nil {
  754. return "", err
  755. }
  756. headers := make(map[string]string)
  757. err = handleOptions(headers, options)
  758. if err != nil {
  759. return "", err
  760. }
  761. return bucket.Client.Conn.signURL(method, bucket.BucketName, objectKey, expiration, params, headers), nil
  762. }
  763. // PutObjectWithURL uploads an object with the URL. If the object exists, it will be overwritten.
  764. // PutObjectWithURL It will not generate minetype according to the key name.
  765. //
  766. // signedURL signed URL.
  767. // reader io.Reader the read instance for reading the data for the upload.
  768. // options the options for uploading the data. The valid options are CacheControl, ContentDisposition, ContentEncoding,
  769. // Expires, ServerSideEncryption, ObjectACL and custom metadata. Check out the following link for details:
  770. // https://help.aliyun.com/document_detail/oss/api-reference/object/PutObject.html
  771. //
  772. // error it's nil if no error, otherwise it's an error object.
  773. //
  774. func (bucket Bucket) PutObjectWithURL(signedURL string, reader io.Reader, options ...Option) error {
  775. resp, err := bucket.DoPutObjectWithURL(signedURL, reader, options)
  776. if err != nil {
  777. return err
  778. }
  779. defer resp.Body.Close()
  780. return err
  781. }
  782. // PutObjectFromFileWithURL uploads an object from a local file with the signed URL.
  783. // PutObjectFromFileWithURL It does not generate mimetype according to object key's name or the local file name.
  784. //
  785. // signedURL the signed URL.
  786. // filePath local file path, such as dirfile.txt, for uploading.
  787. // options options for uploading, same as the options in PutObject function.
  788. //
  789. // error it's nil if no error, otherwise it's an error object.
  790. //
  791. func (bucket Bucket) PutObjectFromFileWithURL(signedURL, filePath string, options ...Option) error {
  792. fd, err := os.Open(filePath)
  793. if err != nil {
  794. return err
  795. }
  796. defer fd.Close()
  797. resp, err := bucket.DoPutObjectWithURL(signedURL, fd, options)
  798. if err != nil {
  799. return err
  800. }
  801. defer resp.Body.Close()
  802. return err
  803. }
  804. // DoPutObjectWithURL is the actual API that does the upload with URL work(internal for SDK)
  805. //
  806. // signedURL the signed URL.
  807. // reader io.Reader the read instance for getting the data to upload.
  808. // options options for uploading.
  809. //
  810. // Response the response object which contains the HTTP response.
  811. // error it's nil if no error, otherwise it's an error object.
  812. //
  813. func (bucket Bucket) DoPutObjectWithURL(signedURL string, reader io.Reader, options []Option) (*Response, error) {
  814. listener := GetProgressListener(options)
  815. params := map[string]interface{}{}
  816. resp, err := bucket.doURL("PUT", signedURL, params, options, reader, listener)
  817. if err != nil {
  818. return nil, err
  819. }
  820. if bucket.GetConfig().IsEnableCRC {
  821. err = CheckCRC(resp, "DoPutObjectWithURL")
  822. if err != nil {
  823. return resp, err
  824. }
  825. }
  826. err = CheckRespCode(resp.StatusCode, []int{http.StatusOK})
  827. return resp, err
  828. }
  829. // GetObjectWithURL downloads the object and returns the reader instance, with the signed URL.
  830. //
  831. // signedURL the signed URL.
  832. // options options for downloading the object. Valid options are IfModifiedSince, IfUnmodifiedSince, IfMatch,
  833. // IfNoneMatch, AcceptEncoding. For more information, check out the following link:
  834. // https://help.aliyun.com/document_detail/oss/api-reference/object/GetObject.html
  835. //
  836. // io.ReadCloser the reader object for getting the data from response. It needs be closed after the usage. It's only valid when error is nil.
  837. // error it's nil if no error, otherwise it's an error object.
  838. //
  839. func (bucket Bucket) GetObjectWithURL(signedURL string, options ...Option) (io.ReadCloser, error) {
  840. result, err := bucket.DoGetObjectWithURL(signedURL, options)
  841. if err != nil {
  842. return nil, err
  843. }
  844. return result.Response, nil
  845. }
  846. // GetObjectToFileWithURL downloads the object into a local file with the signed URL.
  847. //
  848. // signedURL the signed URL
  849. // filePath the local file path to download to.
  850. // options the options for downloading object. Check out the parameter options in function GetObject for the reference.
  851. //
  852. // error it's nil if no error, otherwise it's an error object.
  853. //
  854. func (bucket Bucket) GetObjectToFileWithURL(signedURL, filePath string, options ...Option) error {
  855. tempFilePath := filePath + TempFileSuffix
  856. // Get the object's content
  857. result, err := bucket.DoGetObjectWithURL(signedURL, options)
  858. if err != nil {
  859. return err
  860. }
  861. defer result.Response.Close()
  862. // If the file does not exist, create one. If exists, then overwrite it.
  863. fd, err := os.OpenFile(tempFilePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, FilePermMode)
  864. if err != nil {
  865. return err
  866. }
  867. // Save the data to the file.
  868. _, err = io.Copy(fd, result.Response.Body)
  869. fd.Close()
  870. if err != nil {
  871. return err
  872. }
  873. // Compare the CRC value. If CRC values do not match, return error.
  874. hasRange, _, _ := IsOptionSet(options, HTTPHeaderRange)
  875. encodeOpt, _ := FindOption(options, HTTPHeaderAcceptEncoding, nil)
  876. acceptEncoding := ""
  877. if encodeOpt != nil {
  878. acceptEncoding = encodeOpt.(string)
  879. }
  880. if bucket.GetConfig().IsEnableCRC && !hasRange && acceptEncoding != "gzip" {
  881. result.Response.ClientCRC = result.ClientCRC.Sum64()
  882. err = CheckCRC(result.Response, "GetObjectToFileWithURL")
  883. if err != nil {
  884. os.Remove(tempFilePath)
  885. return err
  886. }
  887. }
  888. return os.Rename(tempFilePath, filePath)
  889. }
  890. // DoGetObjectWithURL is the actual API that downloads the file with the signed URL.
  891. //
  892. // signedURL the signed URL.
  893. // options the options for getting object. Check out parameter options in GetObject for the reference.
  894. //
  895. // GetObjectResult the result object when the error is nil.
  896. // error it's nil if no error, otherwise it's an error object.
  897. //
  898. func (bucket Bucket) DoGetObjectWithURL(signedURL string, options []Option) (*GetObjectResult, error) {
  899. params, _ := GetRawParams(options)
  900. resp, err := bucket.doURL("GET", signedURL, params, options, nil, nil)
  901. if err != nil {
  902. return nil, err
  903. }
  904. result := &GetObjectResult{
  905. Response: resp,
  906. }
  907. // CRC
  908. var crcCalc hash.Hash64
  909. hasRange, _, _ := IsOptionSet(options, HTTPHeaderRange)
  910. if bucket.GetConfig().IsEnableCRC && !hasRange {
  911. crcCalc = crc64.New(CrcTable())
  912. result.ServerCRC = resp.ServerCRC
  913. result.ClientCRC = crcCalc
  914. }
  915. // Progress
  916. listener := GetProgressListener(options)
  917. contentLen, _ := strconv.ParseInt(resp.Headers.Get(HTTPHeaderContentLength), 10, 64)
  918. resp.Body = TeeReader(resp.Body, crcCalc, contentLen, listener, nil)
  919. return result, nil
  920. }
  921. //
  922. // ProcessObject apply process on the specified image file.
  923. //
  924. // The supported process includes resize, rotate, crop, watermark, format,
  925. // udf, customized style, etc.
  926. //
  927. //
  928. // objectKey object key to process.
  929. // process process string, such as "image/resize,w_100|sys/saveas,o_dGVzdC5qcGc,b_dGVzdA"
  930. //
  931. // error it's nil if no error, otherwise it's an error object.
  932. //
  933. func (bucket Bucket) ProcessObject(objectKey string, process string, options ...Option) (ProcessObjectResult, error) {
  934. var out ProcessObjectResult
  935. params, _ := GetRawParams(options)
  936. params["x-oss-process"] = nil
  937. processData := fmt.Sprintf("%v=%v", "x-oss-process", process)
  938. data := strings.NewReader(processData)
  939. resp, err := bucket.do("POST", objectKey, params, nil, data, nil)
  940. if err != nil {
  941. return out, err
  942. }
  943. defer resp.Body.Close()
  944. err = jsonUnmarshal(resp.Body, &out)
  945. return out, err
  946. }
  947. //
  948. // PutObjectTagging add tagging to object
  949. //
  950. // objectKey object key to add tagging
  951. // tagging tagging to be added
  952. //
  953. // error nil if success, otherwise error
  954. //
  955. func (bucket Bucket) PutObjectTagging(objectKey string, tagging Tagging, options ...Option) error {
  956. bs, err := xml.Marshal(tagging)
  957. if err != nil {
  958. return err
  959. }
  960. buffer := new(bytes.Buffer)
  961. buffer.Write(bs)
  962. params, _ := GetRawParams(options)
  963. params["tagging"] = nil
  964. resp, err := bucket.do("PUT", objectKey, params, options, buffer, nil)
  965. if err != nil {
  966. return err
  967. }
  968. defer resp.Body.Close()
  969. return nil
  970. }
  971. //
  972. // GetObjectTagging get tagging of the object
  973. //
  974. // objectKey object key to get tagging
  975. //
  976. // Tagging
  977. // error nil if success, otherwise error
  978. func (bucket Bucket) GetObjectTagging(objectKey string, options ...Option) (GetObjectTaggingResult, error) {
  979. var out GetObjectTaggingResult
  980. params, _ := GetRawParams(options)
  981. params["tagging"] = nil
  982. resp, err := bucket.do("GET", objectKey, params, options, nil, nil)
  983. if err != nil {
  984. return out, err
  985. }
  986. defer resp.Body.Close()
  987. err = xmlUnmarshal(resp.Body, &out)
  988. return out, err
  989. }
  990. //
  991. // DeleteObjectTagging delete object taggging
  992. //
  993. // objectKey object key to delete tagging
  994. //
  995. // error nil if success, otherwise error
  996. //
  997. func (bucket Bucket) DeleteObjectTagging(objectKey string, options ...Option) error {
  998. params, _ := GetRawParams(options)
  999. params["tagging"] = nil
  1000. if objectKey == "" {
  1001. return fmt.Errorf("invalid argument: object name is empty")
  1002. }
  1003. resp, err := bucket.do("DELETE", objectKey, params, options, nil, nil)
  1004. if err != nil {
  1005. return err
  1006. }
  1007. defer resp.Body.Close()
  1008. return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent})
  1009. }
  1010. func (bucket Bucket) OptionsMethod(objectKey string, options ...Option) (http.Header, error) {
  1011. var out http.Header
  1012. resp, err := bucket.do("OPTIONS", objectKey, nil, options, nil, nil)
  1013. if err != nil {
  1014. return out, err
  1015. }
  1016. defer resp.Body.Close()
  1017. out = resp.Headers
  1018. return out, nil
  1019. }
  1020. // public
  1021. func (bucket Bucket) Do(method, objectName string, params map[string]interface{}, options []Option,
  1022. data io.Reader, listener ProgressListener) (*Response, error) {
  1023. return bucket.do(method, objectName, params, options, data, listener)
  1024. }
  1025. // Private
  1026. func (bucket Bucket) do(method, objectName string, params map[string]interface{}, options []Option,
  1027. data io.Reader, listener ProgressListener) (*Response, error) {
  1028. headers := make(map[string]string)
  1029. err := handleOptions(headers, options)
  1030. if err != nil {
  1031. return nil, err
  1032. }
  1033. err = CheckBucketName(bucket.BucketName)
  1034. if len(bucket.BucketName) > 0 && err != nil {
  1035. return nil, err
  1036. }
  1037. resp, err := bucket.Client.Conn.Do(method, bucket.BucketName, objectName,
  1038. params, headers, data, 0, listener)
  1039. // get response header
  1040. respHeader, _ := FindOption(options, responseHeader, nil)
  1041. if respHeader != nil && resp != nil {
  1042. pRespHeader := respHeader.(*http.Header)
  1043. *pRespHeader = resp.Headers
  1044. }
  1045. return resp, err
  1046. }
  1047. func (bucket Bucket) doURL(method HTTPMethod, signedURL string, params map[string]interface{}, options []Option,
  1048. data io.Reader, listener ProgressListener) (*Response, error) {
  1049. headers := make(map[string]string)
  1050. err := handleOptions(headers, options)
  1051. if err != nil {
  1052. return nil, err
  1053. }
  1054. resp, err := bucket.Client.Conn.DoURL(method, signedURL, headers, data, 0, listener)
  1055. // get response header
  1056. respHeader, _ := FindOption(options, responseHeader, nil)
  1057. if respHeader != nil {
  1058. pRespHeader := respHeader.(*http.Header)
  1059. *pRespHeader = resp.Headers
  1060. }
  1061. return resp, err
  1062. }
  1063. func (bucket Bucket) GetConfig() *Config {
  1064. return bucket.Client.Config
  1065. }
  1066. func AddContentType(options []Option, keys ...string) []Option {
  1067. typ := TypeByExtension("")
  1068. for _, key := range keys {
  1069. typ = TypeByExtension(key)
  1070. if typ != "" {
  1071. break
  1072. }
  1073. }
  1074. if typ == "" {
  1075. typ = "application/octet-stream"
  1076. }
  1077. opts := []Option{ContentType(typ)}
  1078. opts = append(opts, options...)
  1079. return opts
  1080. }