bucket.go 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  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. // ListObjectsResult 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. // Recommend to use ListObjectsV2 to replace ListObjects
  525. // ListOListObjectsV2bjects lists the objects under the current bucket.
  526. // ListObjectsResultV2 the return value after operation succeeds (only valid when error is nil).
  527. func (bucket Bucket) ListObjectsV2(options ...Option) (ListObjectsResultV2, error) {
  528. var out ListObjectsResultV2
  529. options = append(options, EncodingType("url"))
  530. options = append(options, ListType(2))
  531. params, err := GetRawParams(options)
  532. if err != nil {
  533. return out, err
  534. }
  535. resp, err := bucket.do("GET", "", params, options, nil, nil)
  536. if err != nil {
  537. return out, err
  538. }
  539. defer resp.Body.Close()
  540. err = xmlUnmarshal(resp.Body, &out)
  541. if err != nil {
  542. return out, err
  543. }
  544. err = decodeListObjectsResultV2(&out)
  545. return out, err
  546. }
  547. // ListObjectVersions lists objects of all versions under the current bucket.
  548. func (bucket Bucket) ListObjectVersions(options ...Option) (ListObjectVersionsResult, error) {
  549. var out ListObjectVersionsResult
  550. options = append(options, EncodingType("url"))
  551. params, err := GetRawParams(options)
  552. if err != nil {
  553. return out, err
  554. }
  555. params["versions"] = nil
  556. resp, err := bucket.do("GET", "", params, options, nil, nil)
  557. if err != nil {
  558. return out, err
  559. }
  560. defer resp.Body.Close()
  561. err = xmlUnmarshal(resp.Body, &out)
  562. if err != nil {
  563. return out, err
  564. }
  565. err = decodeListObjectVersionsResult(&out)
  566. return out, err
  567. }
  568. // SetObjectMeta sets the metadata of the Object.
  569. //
  570. // objectKey object
  571. // options options for setting the metadata. The valid options are CacheControl, ContentDisposition, ContentEncoding, Expires,
  572. // ServerSideEncryption, and custom metadata.
  573. //
  574. // error it's nil if no error, otherwise it's an error object.
  575. //
  576. func (bucket Bucket) SetObjectMeta(objectKey string, options ...Option) error {
  577. options = append(options, MetadataDirective(MetaReplace))
  578. _, err := bucket.CopyObject(objectKey, objectKey, options...)
  579. return err
  580. }
  581. // GetObjectDetailedMeta gets the object's detailed metadata
  582. //
  583. // objectKey object key.
  584. // 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,
  585. // IfMatch, IfNoneMatch. For more details check out https://help.aliyun.com/document_detail/oss/api-reference/object/HeadObject.html
  586. //
  587. // http.Header object meta when error is nil.
  588. // error it's nil if no error, otherwise it's an error object.
  589. //
  590. func (bucket Bucket) GetObjectDetailedMeta(objectKey string, options ...Option) (http.Header, error) {
  591. params, _ := GetRawParams(options)
  592. resp, err := bucket.do("HEAD", objectKey, params, options, nil, nil)
  593. if err != nil {
  594. return nil, err
  595. }
  596. defer resp.Body.Close()
  597. return resp.Headers, nil
  598. }
  599. // GetObjectMeta gets object metadata.
  600. //
  601. // GetObjectMeta is more lightweight than GetObjectDetailedMeta as it only returns basic metadata including ETag
  602. // size, LastModified. The size information is in the HTTP header Content-Length.
  603. //
  604. // objectKey object key
  605. //
  606. // http.Header the object's metadata, valid when error is nil.
  607. // error it's nil if no error, otherwise it's an error object.
  608. //
  609. func (bucket Bucket) GetObjectMeta(objectKey string, options ...Option) (http.Header, error) {
  610. params, _ := GetRawParams(options)
  611. params["objectMeta"] = nil
  612. //resp, err := bucket.do("GET", objectKey, "?objectMeta", "", nil, nil, nil)
  613. resp, err := bucket.do("HEAD", objectKey, params, options, nil, nil)
  614. if err != nil {
  615. return nil, err
  616. }
  617. defer resp.Body.Close()
  618. return resp.Headers, nil
  619. }
  620. // SetObjectACL updates the object's ACL.
  621. //
  622. // Only the bucket's owner could update object's ACL which priority is higher than bucket's ACL.
  623. // For example, if the bucket ACL is private and object's ACL is public-read-write.
  624. // Then object's ACL is used and it means all users could read or write that object.
  625. // When the object's ACL is not set, then bucket's ACL is used as the object's ACL.
  626. //
  627. // Object read operations include GetObject, HeadObject, CopyObject and UploadPartCopy on the source object;
  628. // Object write operations include PutObject, PostObject, AppendObject, DeleteObject, DeleteMultipleObjects,
  629. // CompleteMultipartUpload and CopyObject on target object.
  630. //
  631. // objectKey the target object key (to set the ACL on)
  632. // objectAcl object ACL. Valid options are PrivateACL, PublicReadACL, PublicReadWriteACL.
  633. //
  634. // error it's nil if no error, otherwise it's an error object.
  635. //
  636. func (bucket Bucket) SetObjectACL(objectKey string, objectACL ACLType, options ...Option) error {
  637. options = append(options, ObjectACL(objectACL))
  638. params, _ := GetRawParams(options)
  639. params["acl"] = nil
  640. resp, err := bucket.do("PUT", objectKey, params, options, nil, nil)
  641. if err != nil {
  642. return err
  643. }
  644. defer resp.Body.Close()
  645. return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
  646. }
  647. // GetObjectACL gets object's ACL
  648. //
  649. // objectKey the object to get ACL from.
  650. //
  651. // GetObjectACLResult the result object when error is nil. GetObjectACLResult.Acl is the object ACL.
  652. // error it's nil if no error, otherwise it's an error object.
  653. //
  654. func (bucket Bucket) GetObjectACL(objectKey string, options ...Option) (GetObjectACLResult, error) {
  655. var out GetObjectACLResult
  656. params, _ := GetRawParams(options)
  657. params["acl"] = nil
  658. resp, err := bucket.do("GET", objectKey, params, options, nil, nil)
  659. if err != nil {
  660. return out, err
  661. }
  662. defer resp.Body.Close()
  663. err = xmlUnmarshal(resp.Body, &out)
  664. return out, err
  665. }
  666. // PutSymlink creates a symlink (to point to an existing object)
  667. //
  668. // Symlink cannot point to another symlink.
  669. // When creating a symlink, it does not check the existence of the target file, and does not check if the target file is symlink.
  670. // Neither it checks the caller's permission on the target file. All these checks are deferred to the actual GetObject call via this symlink.
  671. // If trying to add an existing file, as long as the caller has the write permission, the existing one will be overwritten.
  672. // If the x-oss-meta- is specified, it will be added as the metadata of the symlink file.
  673. //
  674. // symObjectKey the symlink object's key.
  675. // targetObjectKey the target object key to point to.
  676. //
  677. // error it's nil if no error, otherwise it's an error object.
  678. //
  679. func (bucket Bucket) PutSymlink(symObjectKey string, targetObjectKey string, options ...Option) error {
  680. options = append(options, symlinkTarget(url.QueryEscape(targetObjectKey)))
  681. params, _ := GetRawParams(options)
  682. params["symlink"] = nil
  683. resp, err := bucket.do("PUT", symObjectKey, params, options, nil, nil)
  684. if err != nil {
  685. return err
  686. }
  687. defer resp.Body.Close()
  688. return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
  689. }
  690. // GetSymlink gets the symlink object with the specified key.
  691. // If the symlink object does not exist, returns 404.
  692. //
  693. // objectKey the symlink object's key.
  694. //
  695. // error it's nil if no error, otherwise it's an error object.
  696. // When error is nil, the target file key is in the X-Oss-Symlink-Target header of the returned object.
  697. //
  698. func (bucket Bucket) GetSymlink(objectKey string, options ...Option) (http.Header, error) {
  699. params, _ := GetRawParams(options)
  700. params["symlink"] = nil
  701. resp, err := bucket.do("GET", objectKey, params, options, nil, nil)
  702. if err != nil {
  703. return nil, err
  704. }
  705. defer resp.Body.Close()
  706. targetObjectKey := resp.Headers.Get(HTTPHeaderOssSymlinkTarget)
  707. targetObjectKey, err = url.QueryUnescape(targetObjectKey)
  708. if err != nil {
  709. return resp.Headers, err
  710. }
  711. resp.Headers.Set(HTTPHeaderOssSymlinkTarget, targetObjectKey)
  712. return resp.Headers, err
  713. }
  714. // RestoreObject restores the object from the archive storage.
  715. //
  716. // An archive object is in cold status by default and it cannot be accessed.
  717. // When restore is called on the cold object, it will become available for access after some time.
  718. // 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.
  719. // By default, the restored object is available for access for one day. After that it will be unavailable again.
  720. // 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.
  721. //
  722. // objectKey object key to restore.
  723. //
  724. // error it's nil if no error, otherwise it's an error object.
  725. //
  726. func (bucket Bucket) RestoreObject(objectKey string, options ...Option) error {
  727. params, _ := GetRawParams(options)
  728. params["restore"] = nil
  729. resp, err := bucket.do("POST", objectKey, params, options, nil, nil)
  730. if err != nil {
  731. return err
  732. }
  733. defer resp.Body.Close()
  734. return CheckRespCode(resp.StatusCode, []int{http.StatusOK, http.StatusAccepted})
  735. }
  736. // RestoreObjectDetail support more features than RestoreObject
  737. func (bucket Bucket) RestoreObjectDetail(objectKey string, restoreConfig RestoreConfiguration, options ...Option) error {
  738. if restoreConfig.Tier == "" {
  739. // Expedited, Standard, Bulk
  740. restoreConfig.Tier = string(RestoreStandard)
  741. }
  742. if restoreConfig.Days == 0 {
  743. restoreConfig.Days = 1
  744. }
  745. bs, err := xml.Marshal(restoreConfig)
  746. if err != nil {
  747. return err
  748. }
  749. buffer := new(bytes.Buffer)
  750. buffer.Write(bs)
  751. contentType := http.DetectContentType(buffer.Bytes())
  752. options = append(options, ContentType(contentType))
  753. params, _ := GetRawParams(options)
  754. params["restore"] = nil
  755. resp, err := bucket.do("POST", objectKey, params, options, buffer, nil)
  756. if err != nil {
  757. return err
  758. }
  759. defer resp.Body.Close()
  760. return CheckRespCode(resp.StatusCode, []int{http.StatusOK, http.StatusAccepted})
  761. }
  762. // SignURL signs the URL. Users could access the object directly with this URL without getting the AK.
  763. //
  764. // objectKey the target object to sign.
  765. // signURLConfig the configuration for the signed URL
  766. //
  767. // string returns the signed URL, when error is nil.
  768. // error it's nil if no error, otherwise it's an error object.
  769. //
  770. func (bucket Bucket) SignURL(objectKey string, method HTTPMethod, expiredInSec int64, options ...Option) (string, error) {
  771. if expiredInSec < 0 {
  772. return "", fmt.Errorf("invalid expires: %d, expires must bigger than 0", expiredInSec)
  773. }
  774. expiration := time.Now().Unix() + expiredInSec
  775. params, err := GetRawParams(options)
  776. if err != nil {
  777. return "", err
  778. }
  779. headers := make(map[string]string)
  780. err = handleOptions(headers, options)
  781. if err != nil {
  782. return "", err
  783. }
  784. return bucket.Client.Conn.signURL(method, bucket.BucketName, objectKey, expiration, params, headers), nil
  785. }
  786. // PutObjectWithURL uploads an object with the URL. If the object exists, it will be overwritten.
  787. // PutObjectWithURL It will not generate minetype according to the key name.
  788. //
  789. // signedURL signed URL.
  790. // reader io.Reader the read instance for reading the data for the upload.
  791. // options the options for uploading the data. The valid options are CacheControl, ContentDisposition, ContentEncoding,
  792. // Expires, ServerSideEncryption, ObjectACL and custom metadata. Check out the following link for details:
  793. // https://help.aliyun.com/document_detail/oss/api-reference/object/PutObject.html
  794. //
  795. // error it's nil if no error, otherwise it's an error object.
  796. //
  797. func (bucket Bucket) PutObjectWithURL(signedURL string, reader io.Reader, options ...Option) error {
  798. resp, err := bucket.DoPutObjectWithURL(signedURL, reader, options)
  799. if err != nil {
  800. return err
  801. }
  802. defer resp.Body.Close()
  803. return err
  804. }
  805. // PutObjectFromFileWithURL uploads an object from a local file with the signed URL.
  806. // PutObjectFromFileWithURL It does not generate mimetype according to object key's name or the local file name.
  807. //
  808. // signedURL the signed URL.
  809. // filePath local file path, such as dirfile.txt, for uploading.
  810. // options options for uploading, same as the options in PutObject function.
  811. //
  812. // error it's nil if no error, otherwise it's an error object.
  813. //
  814. func (bucket Bucket) PutObjectFromFileWithURL(signedURL, filePath string, options ...Option) error {
  815. fd, err := os.Open(filePath)
  816. if err != nil {
  817. return err
  818. }
  819. defer fd.Close()
  820. resp, err := bucket.DoPutObjectWithURL(signedURL, fd, options)
  821. if err != nil {
  822. return err
  823. }
  824. defer resp.Body.Close()
  825. return err
  826. }
  827. // DoPutObjectWithURL is the actual API that does the upload with URL work(internal for SDK)
  828. //
  829. // signedURL the signed URL.
  830. // reader io.Reader the read instance for getting the data to upload.
  831. // options options for uploading.
  832. //
  833. // Response the response object which contains the HTTP response.
  834. // error it's nil if no error, otherwise it's an error object.
  835. //
  836. func (bucket Bucket) DoPutObjectWithURL(signedURL string, reader io.Reader, options []Option) (*Response, error) {
  837. listener := GetProgressListener(options)
  838. params := map[string]interface{}{}
  839. resp, err := bucket.doURL("PUT", signedURL, params, options, reader, listener)
  840. if err != nil {
  841. return nil, err
  842. }
  843. if bucket.GetConfig().IsEnableCRC {
  844. err = CheckCRC(resp, "DoPutObjectWithURL")
  845. if err != nil {
  846. return resp, err
  847. }
  848. }
  849. err = CheckRespCode(resp.StatusCode, []int{http.StatusOK})
  850. return resp, err
  851. }
  852. // GetObjectWithURL downloads the object and returns the reader instance, with the signed URL.
  853. //
  854. // signedURL the signed URL.
  855. // options options for downloading the object. Valid options are IfModifiedSince, IfUnmodifiedSince, IfMatch,
  856. // IfNoneMatch, AcceptEncoding. For more information, check out the following link:
  857. // https://help.aliyun.com/document_detail/oss/api-reference/object/GetObject.html
  858. //
  859. // 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.
  860. // error it's nil if no error, otherwise it's an error object.
  861. //
  862. func (bucket Bucket) GetObjectWithURL(signedURL string, options ...Option) (io.ReadCloser, error) {
  863. result, err := bucket.DoGetObjectWithURL(signedURL, options)
  864. if err != nil {
  865. return nil, err
  866. }
  867. return result.Response, nil
  868. }
  869. // GetObjectToFileWithURL downloads the object into a local file with the signed URL.
  870. //
  871. // signedURL the signed URL
  872. // filePath the local file path to download to.
  873. // options the options for downloading object. Check out the parameter options in function GetObject for the reference.
  874. //
  875. // error it's nil if no error, otherwise it's an error object.
  876. //
  877. func (bucket Bucket) GetObjectToFileWithURL(signedURL, filePath string, options ...Option) error {
  878. tempFilePath := filePath + TempFileSuffix
  879. // Get the object's content
  880. result, err := bucket.DoGetObjectWithURL(signedURL, options)
  881. if err != nil {
  882. return err
  883. }
  884. defer result.Response.Close()
  885. // If the file does not exist, create one. If exists, then overwrite it.
  886. fd, err := os.OpenFile(tempFilePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, FilePermMode)
  887. if err != nil {
  888. return err
  889. }
  890. // Save the data to the file.
  891. _, err = io.Copy(fd, result.Response.Body)
  892. fd.Close()
  893. if err != nil {
  894. return err
  895. }
  896. // Compare the CRC value. If CRC values do not match, return error.
  897. hasRange, _, _ := IsOptionSet(options, HTTPHeaderRange)
  898. encodeOpt, _ := FindOption(options, HTTPHeaderAcceptEncoding, nil)
  899. acceptEncoding := ""
  900. if encodeOpt != nil {
  901. acceptEncoding = encodeOpt.(string)
  902. }
  903. if bucket.GetConfig().IsEnableCRC && !hasRange && acceptEncoding != "gzip" {
  904. result.Response.ClientCRC = result.ClientCRC.Sum64()
  905. err = CheckCRC(result.Response, "GetObjectToFileWithURL")
  906. if err != nil {
  907. os.Remove(tempFilePath)
  908. return err
  909. }
  910. }
  911. return os.Rename(tempFilePath, filePath)
  912. }
  913. // DoGetObjectWithURL is the actual API that downloads the file with the signed URL.
  914. //
  915. // signedURL the signed URL.
  916. // options the options for getting object. Check out parameter options in GetObject for the reference.
  917. //
  918. // GetObjectResult the result object when the error is nil.
  919. // error it's nil if no error, otherwise it's an error object.
  920. //
  921. func (bucket Bucket) DoGetObjectWithURL(signedURL string, options []Option) (*GetObjectResult, error) {
  922. params, _ := GetRawParams(options)
  923. resp, err := bucket.doURL("GET", signedURL, params, options, nil, nil)
  924. if err != nil {
  925. return nil, err
  926. }
  927. result := &GetObjectResult{
  928. Response: resp,
  929. }
  930. // CRC
  931. var crcCalc hash.Hash64
  932. hasRange, _, _ := IsOptionSet(options, HTTPHeaderRange)
  933. if bucket.GetConfig().IsEnableCRC && !hasRange {
  934. crcCalc = crc64.New(CrcTable())
  935. result.ServerCRC = resp.ServerCRC
  936. result.ClientCRC = crcCalc
  937. }
  938. // Progress
  939. listener := GetProgressListener(options)
  940. contentLen, _ := strconv.ParseInt(resp.Headers.Get(HTTPHeaderContentLength), 10, 64)
  941. resp.Body = TeeReader(resp.Body, crcCalc, contentLen, listener, nil)
  942. return result, nil
  943. }
  944. //
  945. // ProcessObject apply process on the specified image file.
  946. //
  947. // The supported process includes resize, rotate, crop, watermark, format,
  948. // udf, customized style, etc.
  949. //
  950. //
  951. // objectKey object key to process.
  952. // process process string, such as "image/resize,w_100|sys/saveas,o_dGVzdC5qcGc,b_dGVzdA"
  953. //
  954. // error it's nil if no error, otherwise it's an error object.
  955. //
  956. func (bucket Bucket) ProcessObject(objectKey string, process string, options ...Option) (ProcessObjectResult, error) {
  957. var out ProcessObjectResult
  958. params, _ := GetRawParams(options)
  959. params["x-oss-process"] = nil
  960. processData := fmt.Sprintf("%v=%v", "x-oss-process", process)
  961. data := strings.NewReader(processData)
  962. resp, err := bucket.do("POST", objectKey, params, nil, data, nil)
  963. if err != nil {
  964. return out, err
  965. }
  966. defer resp.Body.Close()
  967. err = jsonUnmarshal(resp.Body, &out)
  968. return out, err
  969. }
  970. //
  971. // PutObjectTagging add tagging to object
  972. //
  973. // objectKey object key to add tagging
  974. // tagging tagging to be added
  975. //
  976. // error nil if success, otherwise error
  977. //
  978. func (bucket Bucket) PutObjectTagging(objectKey string, tagging Tagging, options ...Option) error {
  979. bs, err := xml.Marshal(tagging)
  980. if err != nil {
  981. return err
  982. }
  983. buffer := new(bytes.Buffer)
  984. buffer.Write(bs)
  985. params, _ := GetRawParams(options)
  986. params["tagging"] = nil
  987. resp, err := bucket.do("PUT", objectKey, params, options, buffer, nil)
  988. if err != nil {
  989. return err
  990. }
  991. defer resp.Body.Close()
  992. return nil
  993. }
  994. //
  995. // GetObjectTagging get tagging of the object
  996. //
  997. // objectKey object key to get tagging
  998. //
  999. // Tagging
  1000. // error nil if success, otherwise error
  1001. func (bucket Bucket) GetObjectTagging(objectKey string, options ...Option) (GetObjectTaggingResult, error) {
  1002. var out GetObjectTaggingResult
  1003. params, _ := GetRawParams(options)
  1004. params["tagging"] = nil
  1005. resp, err := bucket.do("GET", objectKey, params, options, nil, nil)
  1006. if err != nil {
  1007. return out, err
  1008. }
  1009. defer resp.Body.Close()
  1010. err = xmlUnmarshal(resp.Body, &out)
  1011. return out, err
  1012. }
  1013. //
  1014. // DeleteObjectTagging delete object taggging
  1015. //
  1016. // objectKey object key to delete tagging
  1017. //
  1018. // error nil if success, otherwise error
  1019. //
  1020. func (bucket Bucket) DeleteObjectTagging(objectKey string, options ...Option) error {
  1021. params, _ := GetRawParams(options)
  1022. params["tagging"] = nil
  1023. if objectKey == "" {
  1024. return fmt.Errorf("invalid argument: object name is empty")
  1025. }
  1026. resp, err := bucket.do("DELETE", objectKey, params, options, nil, nil)
  1027. if err != nil {
  1028. return err
  1029. }
  1030. defer resp.Body.Close()
  1031. return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent})
  1032. }
  1033. func (bucket Bucket) OptionsMethod(objectKey string, options ...Option) (http.Header, error) {
  1034. var out http.Header
  1035. resp, err := bucket.do("OPTIONS", objectKey, nil, options, nil, nil)
  1036. if err != nil {
  1037. return out, err
  1038. }
  1039. defer resp.Body.Close()
  1040. out = resp.Headers
  1041. return out, nil
  1042. }
  1043. // public
  1044. func (bucket Bucket) Do(method, objectName string, params map[string]interface{}, options []Option,
  1045. data io.Reader, listener ProgressListener) (*Response, error) {
  1046. return bucket.do(method, objectName, params, options, data, listener)
  1047. }
  1048. // Private
  1049. func (bucket Bucket) do(method, objectName string, params map[string]interface{}, options []Option,
  1050. data io.Reader, listener ProgressListener) (*Response, error) {
  1051. headers := make(map[string]string)
  1052. err := handleOptions(headers, options)
  1053. if err != nil {
  1054. return nil, err
  1055. }
  1056. err = CheckBucketName(bucket.BucketName)
  1057. if len(bucket.BucketName) > 0 && err != nil {
  1058. return nil, err
  1059. }
  1060. resp, err := bucket.Client.Conn.Do(method, bucket.BucketName, objectName,
  1061. params, headers, data, 0, listener)
  1062. // get response header
  1063. respHeader, _ := FindOption(options, responseHeader, nil)
  1064. if respHeader != nil && resp != nil {
  1065. pRespHeader := respHeader.(*http.Header)
  1066. *pRespHeader = resp.Headers
  1067. }
  1068. return resp, err
  1069. }
  1070. func (bucket Bucket) doURL(method HTTPMethod, signedURL string, params map[string]interface{}, options []Option,
  1071. data io.Reader, listener ProgressListener) (*Response, error) {
  1072. headers := make(map[string]string)
  1073. err := handleOptions(headers, options)
  1074. if err != nil {
  1075. return nil, err
  1076. }
  1077. resp, err := bucket.Client.Conn.DoURL(method, signedURL, headers, data, 0, listener)
  1078. // get response header
  1079. respHeader, _ := FindOption(options, responseHeader, nil)
  1080. if respHeader != nil {
  1081. pRespHeader := respHeader.(*http.Header)
  1082. *pRespHeader = resp.Headers
  1083. }
  1084. return resp, err
  1085. }
  1086. func (bucket Bucket) GetConfig() *Config {
  1087. return bucket.Client.Config
  1088. }
  1089. func AddContentType(options []Option, keys ...string) []Option {
  1090. typ := TypeByExtension("")
  1091. for _, key := range keys {
  1092. typ = TypeByExtension(key)
  1093. if typ != "" {
  1094. break
  1095. }
  1096. }
  1097. if typ == "" {
  1098. typ = "application/octet-stream"
  1099. }
  1100. opts := []Option{ContentType(typ)}
  1101. opts = append(opts, options...)
  1102. return opts
  1103. }