get_object.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. fmt.Println("size of data is: ", len(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. body, err = bucket.GetObject(objectKey, oss.IfUnmodifiedSince(pastDate))
  67. if err == nil {
  68. HandleError(err)
  69. }
  70. body.Close()
  71. meta, err := bucket.GetObjectDetailedMeta(objectKey)
  72. if err != nil {
  73. HandleError(err)
  74. }
  75. etag := meta.Get(oss.HTTPHeaderEtag)
  76. // Check the content, etag contraint is met, download the file
  77. body, err = bucket.GetObject(objectKey, oss.IfMatch(etag))
  78. if err != nil {
  79. HandleError(err)
  80. }
  81. body.Close()
  82. // Check the content, etag contraint is not met, do not download the file
  83. body, err = bucket.GetObject(objectKey, oss.IfNoneMatch(etag))
  84. if err == nil {
  85. HandleError(err)
  86. }
  87. body.Close()
  88. // Case 6: Big file's multipart download, concurrent and resumable download is supported.
  89. // multipart download with part size 100KB. By default single coroutine is used and no checkpoint
  90. err = bucket.DownloadFile(objectKey, "mynewfile-3.jpg", 100*1024)
  91. if err != nil {
  92. HandleError(err)
  93. }
  94. // Part size is 100K and 3 coroutines are used
  95. err = bucket.DownloadFile(objectKey, "mynewfile-3.jpg", 100*1024, oss.Routines(3))
  96. if err != nil {
  97. HandleError(err)
  98. }
  99. // Part size is 100K and 3 coroutines with checkpoint
  100. err = bucket.DownloadFile(objectKey, "mynewfile-3.jpg", 100*1024, oss.Routines(3), oss.Checkpoint(true, ""))
  101. if err != nil {
  102. HandleError(err)
  103. }
  104. // Specify the checkpoint file path to record which parts have been downloaded.
  105. // This file path can be specified by the 2nd parameter of Checkpoint, it will be the download directory if the file path is empty.
  106. err = bucket.DownloadFile(objectKey, "mynewfile-3.jpg", 100*1024, oss.Checkpoint(true, "mynewfile.cp"))
  107. if err != nil {
  108. HandleError(err)
  109. }
  110. // Case 7: Use GZIP encoding for downloading the file, GetObject/GetObjectToFile are the same.
  111. err = bucket.PutObjectFromFile(objectKey, htmlLocalFile)
  112. if err != nil {
  113. HandleError(err)
  114. }
  115. err = bucket.GetObjectToFile(objectKey, "myhtml.gzip", oss.AcceptEncoding("gzip"))
  116. if err != nil {
  117. HandleError(err)
  118. }
  119. // Delete the object and bucket
  120. err = DeleteTestBucketAndObject(bucketName)
  121. if err != nil {
  122. HandleError(err)
  123. }
  124. fmt.Println("GetObjectSample completed")
  125. }