put_object.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package sample
  2. import (
  3. "bytes"
  4. "encoding/base64"
  5. "encoding/json"
  6. "fmt"
  7. "os"
  8. "strings"
  9. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  10. )
  11. // PutObjectSample illustrates two methods for uploading a file: simple upload and multipart upload.
  12. func PutObjectSample() {
  13. // Create bucket
  14. bucket, err := GetTestBucket(bucketName)
  15. if err != nil {
  16. HandleError(err)
  17. }
  18. var val = "花间一壶酒,独酌无相亲。 举杯邀明月,对影成三人。"
  19. // Case 1: Upload an object from a string
  20. err = bucket.PutObject(objectKey, strings.NewReader(val))
  21. if err != nil {
  22. HandleError(err)
  23. }
  24. // Case 2: Upload an object whose value is a byte[]
  25. err = bucket.PutObject(objectKey, bytes.NewReader([]byte(val)))
  26. if err != nil {
  27. HandleError(err)
  28. }
  29. // Case 3: Upload the local file with file handle, user should open the file at first.
  30. fd, err := os.Open(localFile)
  31. if err != nil {
  32. HandleError(err)
  33. }
  34. defer fd.Close()
  35. err = bucket.PutObject(objectKey, fd)
  36. if err != nil {
  37. HandleError(err)
  38. }
  39. // Case 4: Upload an object with local file name, user need not open the file.
  40. err = bucket.PutObjectFromFile(objectKey, localFile)
  41. if err != nil {
  42. HandleError(err)
  43. }
  44. // Case 5: Upload an object with specified properties, PutObject/PutObjectFromFile/UploadFile also support this feature.
  45. options := []oss.Option{
  46. oss.Expires(futureDate),
  47. oss.ObjectACL(oss.ACLPublicRead),
  48. oss.Meta("myprop", "mypropval"),
  49. }
  50. err = bucket.PutObject(objectKey, strings.NewReader(val), options...)
  51. if err != nil {
  52. HandleError(err)
  53. }
  54. props, err := bucket.GetObjectDetailedMeta(objectKey)
  55. if err != nil {
  56. HandleError(err)
  57. }
  58. fmt.Println("Object Meta:", props)
  59. // Case 6: Upload an object with sever side encrpytion kms and kms id specified
  60. err = bucket.PutObject(objectKey, strings.NewReader(val), oss.ServerSideEncryption("KMS"), oss.ServerSideEncryptionKeyID(kmsID))
  61. if err != nil {
  62. HandleError(err)
  63. }
  64. // Case 7: Upload an object with callback
  65. callbackMap := map[string]string{}
  66. callbackMap["callbackUrl"] = "http://oss-demo.aliyuncs.com:23450"
  67. callbackMap["callbackHost"] = "oss-cn-hangzhou.aliyuncs.com"
  68. callbackMap["callbackBody"] = "filename=${object}&size=${size}&mimeType=${mimeType}"
  69. callbackMap["callbackBodyType"] = "application/x-www-form-urlencoded"
  70. callbackBuffer := bytes.NewBuffer([]byte{})
  71. callbackEncoder := json.NewEncoder(callbackBuffer)
  72. //do not encode '&' to "\u0026"
  73. callbackEncoder.SetEscapeHTML(false)
  74. err = callbackEncoder.Encode(callbackMap)
  75. if err != nil {
  76. HandleError(err)
  77. }
  78. callbackVal := base64.StdEncoding.EncodeToString(callbackBuffer.Bytes())
  79. err = bucket.PutObject(objectKey, strings.NewReader(val), oss.Callback(callbackVal))
  80. if err != nil {
  81. HandleError(err)
  82. }
  83. // Case 8: Big file's multipart upload. It supports concurrent upload with resumable upload.
  84. // multipart upload with 100K as part size. By default 1 coroutine is used and no checkpoint is used.
  85. err = bucket.UploadFile(objectKey, localFile, 100*1024)
  86. if err != nil {
  87. HandleError(err)
  88. }
  89. // Part size is 100K and 3 coroutines are used
  90. err = bucket.UploadFile(objectKey, localFile, 100*1024, oss.Routines(3))
  91. if err != nil {
  92. HandleError(err)
  93. }
  94. // Part size is 100K and 3 coroutines with checkpoint
  95. err = bucket.UploadFile(objectKey, localFile, 100*1024, oss.Routines(3), oss.Checkpoint(true, ""))
  96. if err != nil {
  97. HandleError(err)
  98. }
  99. // Specify the local file path for checkpoint files.
  100. // the 2nd parameter of Checkpoint can specify the file path, when the file path is empty, it will upload the directory.
  101. err = bucket.UploadFile(objectKey, localFile, 100*1024, oss.Checkpoint(true, localFile+".cp"))
  102. if err != nil {
  103. HandleError(err)
  104. }
  105. // Case 9: Set the storage classes.OSS provides three storage classes: Standard, Infrequent Access, and Archive.
  106. // Supported APIs: PutObject, CopyObject, UploadFile, AppendObject...
  107. err = bucket.PutObject(objectKey, strings.NewReader(val), oss.ObjectStorageClass("IA"))
  108. if err != nil {
  109. HandleError(err)
  110. }
  111. // Upload a local file, and set the object's storage-class to 'Archive'.
  112. err = bucket.UploadFile(objectKey, localFile, 100*1024, oss.ObjectStorageClass("Archive"))
  113. if err != nil {
  114. HandleError(err)
  115. }
  116. // Delete object and bucket
  117. err = DeleteTestBucketAndObject(bucketName)
  118. if err != nil {
  119. HandleError(err)
  120. }
  121. fmt.Println("PutObjectSample completed")
  122. }