put_object.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package sample
  2. import (
  3. "bytes"
  4. "fmt"
  5. "os"
  6. "strings"
  7. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  8. )
  9. // PutObjectSample illustrates two methods for uploading a file: simple upload and multipart upload.
  10. func PutObjectSample() {
  11. // Create bucket
  12. bucket, err := GetTestBucket(bucketName)
  13. if err != nil {
  14. HandleError(err)
  15. }
  16. var val = "花间一壶酒,独酌无相亲。 举杯邀明月,对影成三人。"
  17. // Case 1: Upload an object from a string
  18. err = bucket.PutObject(objectKey, strings.NewReader(val))
  19. if err != nil {
  20. HandleError(err)
  21. }
  22. // Case 2: Upload an object whose value is a byte[]
  23. err = bucket.PutObject(objectKey, bytes.NewReader([]byte(val)))
  24. if err != nil {
  25. HandleError(err)
  26. }
  27. // Case 3: Upload the local file with file handle, user should open the file at first.
  28. fd, err := os.Open(localFile)
  29. if err != nil {
  30. HandleError(err)
  31. }
  32. defer fd.Close()
  33. err = bucket.PutObject(objectKey, fd)
  34. if err != nil {
  35. HandleError(err)
  36. }
  37. // Case 4: Upload an object with local file name, user need not open the file.
  38. err = bucket.PutObjectFromFile(objectKey, localFile)
  39. if err != nil {
  40. HandleError(err)
  41. }
  42. // Case 5: Upload an object with specified properties, PutObject/PutObjectFromFile/UploadFile also support this feature.
  43. options := []oss.Option{
  44. oss.Expires(futureDate),
  45. oss.ObjectACL(oss.ACLPublicRead),
  46. oss.Meta("myprop", "mypropval"),
  47. }
  48. err = bucket.PutObject(objectKey, strings.NewReader(val), options...)
  49. if err != nil {
  50. HandleError(err)
  51. }
  52. props, err := bucket.GetObjectDetailedMeta(objectKey)
  53. if err != nil {
  54. HandleError(err)
  55. }
  56. fmt.Println("Object Meta:", props)
  57. // Case 6: Big file's multipart upload. It supports concurrent upload with resumable upload.
  58. // multipart upload with 100K as part size. By default 1 coroutine is used and no checkpoint is used.
  59. err = bucket.UploadFile(objectKey, localFile, 100*1024)
  60. if err != nil {
  61. HandleError(err)
  62. }
  63. // Part size is 100K and 3 coroutines are used
  64. err = bucket.UploadFile(objectKey, localFile, 100*1024, oss.Routines(3))
  65. if err != nil {
  66. HandleError(err)
  67. }
  68. // Part size is 100K and 3 coroutines with checkpoint
  69. err = bucket.UploadFile(objectKey, localFile, 100*1024, oss.Routines(3), oss.Checkpoint(true, ""))
  70. if err != nil {
  71. HandleError(err)
  72. }
  73. // Specify the local file path for checkpoint files.
  74. // the 2nd parameter of Checkpoint can specify the file path, when the file path is empty, it will upload the directory.
  75. err = bucket.UploadFile(objectKey, localFile, 100*1024, oss.Checkpoint(true, localFile+".cp"))
  76. if err != nil {
  77. HandleError(err)
  78. }
  79. // Delete object and bucket
  80. err = DeleteTestBucketAndObject(bucketName)
  81. if err != nil {
  82. HandleError(err)
  83. }
  84. fmt.Println("PutObjectSample completed")
  85. }