get_object.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 in the range of object.
  34. body, err = bucket.GetObject(objectKey, oss.Range(15, 19))
  35. if err != nil {
  36. HandleError(err)
  37. }
  38. data, err = ioutil.ReadAll(body)
  39. body.Close()
  40. fmt.Println("the range of data is: ", string(data))
  41. // Case 3: Download the object to byte array. This is for small object.
  42. buf := new(bytes.Buffer)
  43. body, err = bucket.GetObject(objectKey)
  44. if err != nil {
  45. HandleError(err)
  46. }
  47. io.Copy(buf, body)
  48. body.Close()
  49. // Case 4: Download the object to local file. The file handle needs to be specified
  50. fd, err := os.OpenFile("mynewfile-1.jpg", os.O_WRONLY|os.O_CREATE, 0660)
  51. if err != nil {
  52. HandleError(err)
  53. }
  54. defer fd.Close()
  55. body, err = bucket.GetObject(objectKey)
  56. if err != nil {
  57. HandleError(err)
  58. }
  59. io.Copy(fd, body)
  60. body.Close()
  61. // Case 5: Download the object to local file with file name specified
  62. err = bucket.GetObjectToFile(objectKey, "mynewfile-2.jpg")
  63. if err != nil {
  64. HandleError(err)
  65. }
  66. // Case 6: Get the object with contraints. When contraints are met, download the file. Otherwise return precondition error
  67. // last modified time constraint is met, download the file
  68. body, err = bucket.GetObject(objectKey, oss.IfModifiedSince(pastDate))
  69. if err != nil {
  70. HandleError(err)
  71. }
  72. body.Close()
  73. // Last modified time contraint is not met, do not download the file
  74. _, err = bucket.GetObject(objectKey, oss.IfUnmodifiedSince(pastDate))
  75. if err == nil {
  76. HandleError(fmt.Errorf("This result is not the expected result"))
  77. }
  78. // body.Close()
  79. meta, err := bucket.GetObjectDetailedMeta(objectKey)
  80. if err != nil {
  81. HandleError(err)
  82. }
  83. etag := meta.Get(oss.HTTPHeaderEtag)
  84. // Check the content, etag contraint is met, download the file
  85. body, err = bucket.GetObject(objectKey, oss.IfMatch(etag))
  86. if err != nil {
  87. HandleError(err)
  88. }
  89. body.Close()
  90. // Check the content, etag contraint is not met, do not download the file
  91. _, err = bucket.GetObject(objectKey, oss.IfNoneMatch(etag))
  92. if err == nil {
  93. HandleError(fmt.Errorf("This result is not the expected result"))
  94. }
  95. // body.Close()
  96. // Case 7: Big file's multipart download, concurrent and resumable download is supported.
  97. // multipart download with part size 100KB. By default single coroutine is used and no checkpoint
  98. err = bucket.DownloadFile(objectKey, "mynewfile-3.jpg", 100*1024)
  99. if err != nil {
  100. HandleError(err)
  101. }
  102. // Part size is 100K and 3 coroutines are used
  103. err = bucket.DownloadFile(objectKey, "mynewfile-3.jpg", 100*1024, oss.Routines(3))
  104. if err != nil {
  105. HandleError(err)
  106. }
  107. // Part size is 100K and 3 coroutines with checkpoint
  108. err = bucket.DownloadFile(objectKey, "mynewfile-3.jpg", 100*1024, oss.Routines(3), oss.Checkpoint(true, ""))
  109. if err != nil {
  110. HandleError(err)
  111. }
  112. // Specify the checkpoint file path to record which parts have been downloaded.
  113. // This file path can be specified by the 2nd parameter of Checkpoint, it will be the download directory if the file path is empty.
  114. err = bucket.DownloadFile(objectKey, "mynewfile-3.jpg", 100*1024, oss.Checkpoint(true, "mynewfile.cp"))
  115. if err != nil {
  116. HandleError(err)
  117. }
  118. // Case 8: Use GZIP encoding for downloading the file, GetObject/GetObjectToFile are the same.
  119. err = bucket.PutObjectFromFile(objectKey, htmlLocalFile)
  120. if err != nil {
  121. HandleError(err)
  122. }
  123. err = bucket.GetObjectToFile(objectKey, "myhtml.gzip", oss.AcceptEncoding("gzip"))
  124. if err != nil {
  125. HandleError(err)
  126. }
  127. // Delete the object and bucket
  128. err = DeleteTestBucketAndObject(bucketName)
  129. if err != nil {
  130. HandleError(err)
  131. }
  132. fmt.Println("GetObjectSample completed")
  133. }