bucket.go 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  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. // SignURL signs the URL. Users could access the object directly with this URL without getting the AK.
  714. //
  715. // objectKey the target object to sign.
  716. // signURLConfig the configuration for the signed URL
  717. //
  718. // string returns the signed URL, when error is nil.
  719. // error it's nil if no error, otherwise it's an error object.
  720. //
  721. func (bucket Bucket) SignURL(objectKey string, method HTTPMethod, expiredInSec int64, options ...Option) (string, error) {
  722. if expiredInSec < 0 {
  723. return "", fmt.Errorf("invalid expires: %d, expires must bigger than 0", expiredInSec)
  724. }
  725. expiration := time.Now().Unix() + expiredInSec
  726. params, err := GetRawParams(options)
  727. if err != nil {
  728. return "", err
  729. }
  730. headers := make(map[string]string)
  731. err = handleOptions(headers, options)
  732. if err != nil {
  733. return "", err
  734. }
  735. return bucket.Client.Conn.signURL(method, bucket.BucketName, objectKey, expiration, params, headers), nil
  736. }
  737. // PutObjectWithURL uploads an object with the URL. If the object exists, it will be overwritten.
  738. // PutObjectWithURL It will not generate minetype according to the key name.
  739. //
  740. // signedURL signed URL.
  741. // reader io.Reader the read instance for reading the data for the upload.
  742. // options the options for uploading the data. The valid options are CacheControl, ContentDisposition, ContentEncoding,
  743. // Expires, ServerSideEncryption, ObjectACL and custom metadata. Check out the following link for details:
  744. // https://help.aliyun.com/document_detail/oss/api-reference/object/PutObject.html
  745. //
  746. // error it's nil if no error, otherwise it's an error object.
  747. //
  748. func (bucket Bucket) PutObjectWithURL(signedURL string, reader io.Reader, options ...Option) error {
  749. resp, err := bucket.DoPutObjectWithURL(signedURL, reader, options)
  750. if err != nil {
  751. return err
  752. }
  753. defer resp.Body.Close()
  754. return err
  755. }
  756. // PutObjectFromFileWithURL uploads an object from a local file with the signed URL.
  757. // PutObjectFromFileWithURL It does not generate mimetype according to object key's name or the local file name.
  758. //
  759. // signedURL the signed URL.
  760. // filePath local file path, such as dirfile.txt, for uploading.
  761. // options options for uploading, same as the options in PutObject function.
  762. //
  763. // error it's nil if no error, otherwise it's an error object.
  764. //
  765. func (bucket Bucket) PutObjectFromFileWithURL(signedURL, filePath string, options ...Option) error {
  766. fd, err := os.Open(filePath)
  767. if err != nil {
  768. return err
  769. }
  770. defer fd.Close()
  771. resp, err := bucket.DoPutObjectWithURL(signedURL, fd, options)
  772. if err != nil {
  773. return err
  774. }
  775. defer resp.Body.Close()
  776. return err
  777. }
  778. // DoPutObjectWithURL is the actual API that does the upload with URL work(internal for SDK)
  779. //
  780. // signedURL the signed URL.
  781. // reader io.Reader the read instance for getting the data to upload.
  782. // options options for uploading.
  783. //
  784. // Response the response object which contains the HTTP response.
  785. // error it's nil if no error, otherwise it's an error object.
  786. //
  787. func (bucket Bucket) DoPutObjectWithURL(signedURL string, reader io.Reader, options []Option) (*Response, error) {
  788. listener := GetProgressListener(options)
  789. params := map[string]interface{}{}
  790. resp, err := bucket.doURL("PUT", signedURL, params, options, reader, listener)
  791. if err != nil {
  792. return nil, err
  793. }
  794. if bucket.GetConfig().IsEnableCRC {
  795. err = CheckCRC(resp, "DoPutObjectWithURL")
  796. if err != nil {
  797. return resp, err
  798. }
  799. }
  800. err = CheckRespCode(resp.StatusCode, []int{http.StatusOK})
  801. return resp, err
  802. }
  803. // GetObjectWithURL downloads the object and returns the reader instance, with the signed URL.
  804. //
  805. // signedURL the signed URL.
  806. // options options for downloading the object. Valid options are IfModifiedSince, IfUnmodifiedSince, IfMatch,
  807. // IfNoneMatch, AcceptEncoding. For more information, check out the following link:
  808. // https://help.aliyun.com/document_detail/oss/api-reference/object/GetObject.html
  809. //
  810. // 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.
  811. // error it's nil if no error, otherwise it's an error object.
  812. //
  813. func (bucket Bucket) GetObjectWithURL(signedURL string, options ...Option) (io.ReadCloser, error) {
  814. result, err := bucket.DoGetObjectWithURL(signedURL, options)
  815. if err != nil {
  816. return nil, err
  817. }
  818. return result.Response, nil
  819. }
  820. // GetObjectToFileWithURL downloads the object into a local file with the signed URL.
  821. //
  822. // signedURL the signed URL
  823. // filePath the local file path to download to.
  824. // options the options for downloading object. Check out the parameter options in function GetObject for the reference.
  825. //
  826. // error it's nil if no error, otherwise it's an error object.
  827. //
  828. func (bucket Bucket) GetObjectToFileWithURL(signedURL, filePath string, options ...Option) error {
  829. tempFilePath := filePath + TempFileSuffix
  830. // Get the object's content
  831. result, err := bucket.DoGetObjectWithURL(signedURL, options)
  832. if err != nil {
  833. return err
  834. }
  835. defer result.Response.Close()
  836. // If the file does not exist, create one. If exists, then overwrite it.
  837. fd, err := os.OpenFile(tempFilePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, FilePermMode)
  838. if err != nil {
  839. return err
  840. }
  841. // Save the data to the file.
  842. _, err = io.Copy(fd, result.Response.Body)
  843. fd.Close()
  844. if err != nil {
  845. return err
  846. }
  847. // Compare the CRC value. If CRC values do not match, return error.
  848. hasRange, _, _ := IsOptionSet(options, HTTPHeaderRange)
  849. encodeOpt, _ := FindOption(options, HTTPHeaderAcceptEncoding, nil)
  850. acceptEncoding := ""
  851. if encodeOpt != nil {
  852. acceptEncoding = encodeOpt.(string)
  853. }
  854. if bucket.GetConfig().IsEnableCRC && !hasRange && acceptEncoding != "gzip" {
  855. result.Response.ClientCRC = result.ClientCRC.Sum64()
  856. err = CheckCRC(result.Response, "GetObjectToFileWithURL")
  857. if err != nil {
  858. os.Remove(tempFilePath)
  859. return err
  860. }
  861. }
  862. return os.Rename(tempFilePath, filePath)
  863. }
  864. // DoGetObjectWithURL is the actual API that downloads the file with the signed URL.
  865. //
  866. // signedURL the signed URL.
  867. // options the options for getting object. Check out parameter options in GetObject for the reference.
  868. //
  869. // GetObjectResult the result object when the error is nil.
  870. // error it's nil if no error, otherwise it's an error object.
  871. //
  872. func (bucket Bucket) DoGetObjectWithURL(signedURL string, options []Option) (*GetObjectResult, error) {
  873. params, _ := GetRawParams(options)
  874. resp, err := bucket.doURL("GET", signedURL, params, options, nil, nil)
  875. if err != nil {
  876. return nil, err
  877. }
  878. result := &GetObjectResult{
  879. Response: resp,
  880. }
  881. // CRC
  882. var crcCalc hash.Hash64
  883. hasRange, _, _ := IsOptionSet(options, HTTPHeaderRange)
  884. if bucket.GetConfig().IsEnableCRC && !hasRange {
  885. crcCalc = crc64.New(CrcTable())
  886. result.ServerCRC = resp.ServerCRC
  887. result.ClientCRC = crcCalc
  888. }
  889. // Progress
  890. listener := GetProgressListener(options)
  891. contentLen, _ := strconv.ParseInt(resp.Headers.Get(HTTPHeaderContentLength), 10, 64)
  892. resp.Body = TeeReader(resp.Body, crcCalc, contentLen, listener, nil)
  893. return result, nil
  894. }
  895. //
  896. // ProcessObject apply process on the specified image file.
  897. //
  898. // The supported process includes resize, rotate, crop, watermark, format,
  899. // udf, customized style, etc.
  900. //
  901. //
  902. // objectKey object key to process.
  903. // process process string, such as "image/resize,w_100|sys/saveas,o_dGVzdC5qcGc,b_dGVzdA"
  904. //
  905. // error it's nil if no error, otherwise it's an error object.
  906. //
  907. func (bucket Bucket) ProcessObject(objectKey string, process string, options ...Option) (ProcessObjectResult, error) {
  908. var out ProcessObjectResult
  909. params, _ := GetRawParams(options)
  910. params["x-oss-process"] = nil
  911. processData := fmt.Sprintf("%v=%v", "x-oss-process", process)
  912. data := strings.NewReader(processData)
  913. resp, err := bucket.do("POST", objectKey, params, nil, data, nil)
  914. if err != nil {
  915. return out, err
  916. }
  917. defer resp.Body.Close()
  918. err = jsonUnmarshal(resp.Body, &out)
  919. return out, err
  920. }
  921. //
  922. // PutObjectTagging add tagging to object
  923. //
  924. // objectKey object key to add tagging
  925. // tagging tagging to be added
  926. //
  927. // error nil if success, otherwise error
  928. //
  929. func (bucket Bucket) PutObjectTagging(objectKey string, tagging Tagging, options ...Option) error {
  930. bs, err := xml.Marshal(tagging)
  931. if err != nil {
  932. return err
  933. }
  934. buffer := new(bytes.Buffer)
  935. buffer.Write(bs)
  936. params, _ := GetRawParams(options)
  937. params["tagging"] = nil
  938. resp, err := bucket.do("PUT", objectKey, params, options, buffer, nil)
  939. if err != nil {
  940. return err
  941. }
  942. defer resp.Body.Close()
  943. return nil
  944. }
  945. //
  946. // GetObjectTagging get tagging of the object
  947. //
  948. // objectKey object key to get tagging
  949. //
  950. // Tagging
  951. // error nil if success, otherwise error
  952. func (bucket Bucket) GetObjectTagging(objectKey string, options ...Option) (GetObjectTaggingResult, error) {
  953. var out GetObjectTaggingResult
  954. params, _ := GetRawParams(options)
  955. params["tagging"] = nil
  956. resp, err := bucket.do("GET", objectKey, params, options, nil, nil)
  957. if err != nil {
  958. return out, err
  959. }
  960. defer resp.Body.Close()
  961. err = xmlUnmarshal(resp.Body, &out)
  962. return out, err
  963. }
  964. //
  965. // DeleteObjectTagging delete object taggging
  966. //
  967. // objectKey object key to delete tagging
  968. //
  969. // error nil if success, otherwise error
  970. //
  971. func (bucket Bucket) DeleteObjectTagging(objectKey string, options ...Option) error {
  972. params, _ := GetRawParams(options)
  973. params["tagging"] = nil
  974. if objectKey == "" {
  975. return fmt.Errorf("invalid argument: object name is empty")
  976. }
  977. resp, err := bucket.do("DELETE", objectKey, params, options, nil, nil)
  978. if err != nil {
  979. return err
  980. }
  981. defer resp.Body.Close()
  982. return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent})
  983. }
  984. func (bucket Bucket) OptionsMethod(objectKey string, options ...Option) (http.Header, error) {
  985. var out http.Header
  986. resp, err := bucket.do("OPTIONS", objectKey, nil, options, nil, nil)
  987. if err != nil {
  988. return out, err
  989. }
  990. defer resp.Body.Close()
  991. out = resp.Headers
  992. return out, nil
  993. }
  994. // public
  995. func (bucket Bucket) Do(method, objectName string, params map[string]interface{}, options []Option,
  996. data io.Reader, listener ProgressListener) (*Response, error) {
  997. return bucket.do(method, objectName, params, options, data, listener)
  998. }
  999. // Private
  1000. func (bucket Bucket) do(method, objectName string, params map[string]interface{}, options []Option,
  1001. data io.Reader, listener ProgressListener) (*Response, error) {
  1002. headers := make(map[string]string)
  1003. err := handleOptions(headers, options)
  1004. if err != nil {
  1005. return nil, err
  1006. }
  1007. resp, err := bucket.Client.Conn.Do(method, bucket.BucketName, objectName,
  1008. params, headers, data, 0, listener)
  1009. // get response header
  1010. respHeader, _ := FindOption(options, responseHeader, nil)
  1011. if respHeader != nil {
  1012. pRespHeader := respHeader.(*http.Header)
  1013. *pRespHeader = resp.Headers
  1014. }
  1015. return resp, err
  1016. }
  1017. func (bucket Bucket) doURL(method HTTPMethod, signedURL string, params map[string]interface{}, options []Option,
  1018. data io.Reader, listener ProgressListener) (*Response, error) {
  1019. headers := make(map[string]string)
  1020. err := handleOptions(headers, options)
  1021. if err != nil {
  1022. return nil, err
  1023. }
  1024. resp, err := bucket.Client.Conn.DoURL(method, signedURL, headers, data, 0, listener)
  1025. // get response header
  1026. respHeader, _ := FindOption(options, responseHeader, nil)
  1027. if respHeader != nil {
  1028. pRespHeader := respHeader.(*http.Header)
  1029. *pRespHeader = resp.Headers
  1030. }
  1031. return resp, err
  1032. }
  1033. func (bucket Bucket) GetConfig() *Config {
  1034. return bucket.Client.Config
  1035. }
  1036. func AddContentType(options []Option, keys ...string) []Option {
  1037. typ := TypeByExtension("")
  1038. for _, key := range keys {
  1039. typ = TypeByExtension(key)
  1040. if typ != "" {
  1041. break
  1042. }
  1043. }
  1044. if typ == "" {
  1045. typ = "application/octet-stream"
  1046. }
  1047. opts := []Option{ContentType(typ)}
  1048. opts = append(opts, options...)
  1049. return opts
  1050. }