get_object.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package sample
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "os"
  8. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  9. )
  10. // GetObjectSample shows the streaming download, range download and resumable download.
  11. func GetObjectSample() {
  12. // Create bucket
  13. bucket, err := GetTestBucket(bucketName)
  14. if err != nil {
  15. HandleError(err)
  16. }
  17. // Upload the object
  18. err = bucket.PutObjectFromFile(objectKey, localFile)
  19. if err != nil {
  20. HandleError(err)
  21. }
  22. // Case 1: Download the object into ReadCloser(). The body needs to be closed
  23. body, err := bucket.GetObject(objectKey)
  24. if err != nil {
  25. HandleError(err)
  26. }
  27. data, err := ioutil.ReadAll(body)
  28. body.Close()
  29. if err != nil {
  30. HandleError(err)
  31. }
  32. data = data // use data
  33. // Case 2: Download the object to byte array. This is for small object.
  34. buf := new(bytes.Buffer)
  35. body, err = bucket.GetObject(objectKey)
  36. if err != nil {
  37. HandleError(err)
  38. }
  39. io.Copy(buf, body)
  40. body.Close()
  41. // Case 3: Download the object to local file. The file handle needs to be specified
  42. fd, err := os.OpenFile("mynewfile-1.jpg", os.O_WRONLY|os.O_CREATE, 0660)
  43. if err != nil {
  44. HandleError(err)
  45. }
  46. defer fd.Close()
  47. body, err = bucket.GetObject(objectKey)
  48. if err != nil {
  49. HandleError(err)
  50. }
  51. io.Copy(fd, body)
  52. body.Close()
  53. // Case 4: Download the object to local file with file name specified
  54. err = bucket.GetObjectToFile(objectKey, "mynewfile-2.jpg")
  55. if err != nil {
  56. HandleError(err)
  57. }
  58. // Case 5: Get the object with contraints. When contraints are met, download the file. Otherwise return precondition error
  59. // last modified time constraint is met, download the file
  60. body, err = bucket.GetObject(objectKey, oss.IfModifiedSince(pastDate))
  61. if err != nil {
  62. HandleError(err)
  63. }
  64. body.Close()
  65. // Last modified time contraint is not met, do not download the file
  66. _, err = bucket.GetObject(objectKey, oss.IfUnmodifiedSince(pastDate))
  67. if err == nil {
  68. HandleError(err)
  69. }
  70. meta, err := bucket.GetObjectDetailedMeta(objectKey)
  71. if err != nil {
  72. HandleError(err)
  73. }
  74. etag := meta.Get(oss.HTTPHeaderEtag)
  75. // Check the content, etag contraint is met, download the file
  76. body, err = bucket.GetObject(objectKey, oss.IfMatch(etag))
  77. if err != nil {
  78. HandleError(err)
  79. }
  80. body.Close()
  81. // Check the content, etag contraint is not met, do not download the file
  82. body, err = bucket.GetObject(objectKey, oss.IfNoneMatch(etag))
  83. if err == nil {
  84. HandleError(err)
  85. }
  86. // Case 6: Big file's multipart download, concurrent and resumable download is supported.
  87. // multipart download with part size 100KB. By default single coroutine is used and no checkpoint
  88. err = bucket.DownloadFile(objectKey, "mynewfile-3.jpg", 100*1024)
  89. if err != nil {
  90. HandleError(err)
  91. }
  92. // Part size is 100K and 3 coroutines are used
  93. err = bucket.DownloadFile(objectKey, "mynewfile-3.jpg", 100*1024, oss.Routines(3))
  94. if err != nil {
  95. HandleError(err)
  96. }
  97. // Part size is 100K and 3 coroutines with checkpoint
  98. err = bucket.DownloadFile(objectKey, "mynewfile-3.jpg", 100*1024, oss.Routines(3), oss.Checkpoint(true, ""))
  99. if err != nil {
  100. HandleError(err)
  101. }
  102. // Specify the checkpoint file path to record which parts have been downloaded.
  103. // This file path can be specified by the 2nd parameter of Checkpoint, it will be the download directory if the file path is empty.
  104. err = bucket.DownloadFile(objectKey, "mynewfile-3.jpg", 100*1024, oss.Checkpoint(true, "mynewfile.cp"))
  105. if err != nil {
  106. HandleError(err)
  107. }
  108. // Case 7: Use GZIP encoding for downloading the file, GetObject/GetObjectToFile are the same.
  109. err = bucket.PutObjectFromFile(objectKey, htmlLocalFile)
  110. if err != nil {
  111. HandleError(err)
  112. }
  113. err = bucket.GetObjectToFile(objectKey, "myhtml.gzip", oss.AcceptEncoding("gzip"))
  114. if err != nil {
  115. HandleError(err)
  116. }
  117. // Delete the object and bucket
  118. err = DeleteTestBucketAndObject(bucketName)
  119. if err != nil {
  120. HandleError(err)
  121. }
  122. fmt.Println("GetObjectSample completed")
  123. }