append_object.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Package sample examples
  2. package sample
  3. import (
  4. "bytes"
  5. "fmt"
  6. "io/ioutil"
  7. "os"
  8. "strconv"
  9. "strings"
  10. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  11. )
  12. // AppendObjectSample shows the append file's usage
  13. func AppendObjectSample() {
  14. // Create bucket
  15. bucket, err := GetTestBucket(bucketName)
  16. if err != nil {
  17. HandleError(err)
  18. }
  19. err = bucket.DeleteObject(objectKey)
  20. var str = "弃我去者,昨日之日不可留。 乱我心者,今日之日多烦忧!"
  21. var nextPos int64
  22. // Case 1: Append a string to the object
  23. // The first append position is 0 and the return value is for the next append's position.
  24. nextPos, err = bucket.AppendObject(objectKey, strings.NewReader(str), nextPos)
  25. if err != nil {
  26. HandleError(err)
  27. }
  28. // Second append
  29. nextPos, err = bucket.AppendObject(objectKey, strings.NewReader(str), nextPos)
  30. if err != nil {
  31. HandleError(err)
  32. }
  33. // Download
  34. body, err := bucket.GetObject(objectKey)
  35. if err != nil {
  36. HandleError(err)
  37. }
  38. data, err := ioutil.ReadAll(body)
  39. body.Close()
  40. if err != nil {
  41. HandleError(err)
  42. }
  43. fmt.Println(objectKey, ":", string(data))
  44. err = bucket.DeleteObject(objectKey)
  45. if err != nil {
  46. HandleError(err)
  47. }
  48. // Case 2: Append byte array to the object
  49. nextPos = 0
  50. // The first append position is 0, and the return value is for the next append's position.
  51. nextPos, err = bucket.AppendObject(objectKey, bytes.NewReader([]byte(str)), nextPos)
  52. if err != nil {
  53. HandleError(err)
  54. }
  55. // Second append
  56. nextPos, err = bucket.AppendObject(objectKey, bytes.NewReader([]byte(str)), nextPos)
  57. if err != nil {
  58. HandleError(err)
  59. }
  60. // Download
  61. body, err = bucket.GetObject(objectKey)
  62. if err != nil {
  63. HandleError(err)
  64. }
  65. data, err = ioutil.ReadAll(body)
  66. body.Close()
  67. if err != nil {
  68. HandleError(err)
  69. }
  70. fmt.Println(objectKey, ":", string(data))
  71. err = bucket.DeleteObject(objectKey)
  72. if err != nil {
  73. HandleError(err)
  74. }
  75. // Case 3: Append a local file to the object
  76. fd, err := os.Open(localFile)
  77. if err != nil {
  78. HandleError(err)
  79. }
  80. defer fd.Close()
  81. nextPos = 0
  82. nextPos, err = bucket.AppendObject(objectKey, fd, nextPos)
  83. if err != nil {
  84. HandleError(err)
  85. }
  86. // Case 4: Get the next append position by GetObjectDetailedMeta
  87. props, err := bucket.GetObjectDetailedMeta(objectKey)
  88. nextPos, err = strconv.ParseInt(props.Get(oss.HTTPHeaderOssNextAppendPosition), 10, 64)
  89. if err != nil {
  90. HandleError(err)
  91. }
  92. nextPos, err = bucket.AppendObject(objectKey, strings.NewReader(str), nextPos)
  93. if err != nil {
  94. HandleError(err)
  95. }
  96. err = bucket.DeleteObject(objectKey)
  97. if err != nil {
  98. HandleError(err)
  99. }
  100. // Case 5: Specify the object properties for the first append, including the "x-oss-meta"'s custom metadata.
  101. options := []oss.Option{
  102. oss.Expires(futureDate),
  103. oss.ObjectACL(oss.ACLPublicRead),
  104. oss.Meta("myprop", "mypropval")}
  105. nextPos = 0
  106. fd.Seek(0, os.SEEK_SET)
  107. nextPos, err = bucket.AppendObject(objectKey, strings.NewReader(str), nextPos, options...)
  108. if err != nil {
  109. HandleError(err)
  110. }
  111. // Second append
  112. fd.Seek(0, os.SEEK_SET)
  113. nextPos, err = bucket.AppendObject(objectKey, strings.NewReader(str), nextPos)
  114. if err != nil {
  115. HandleError(err)
  116. }
  117. props, err = bucket.GetObjectDetailedMeta(objectKey)
  118. if err != nil {
  119. HandleError(err)
  120. }
  121. fmt.Println("myprop:", props.Get("x-oss-meta-myprop"))
  122. goar, err := bucket.GetObjectACL(objectKey)
  123. if err != nil {
  124. HandleError(err)
  125. }
  126. fmt.Println("Object ACL:", goar.ACL)
  127. // Case 6: Set the storage classes.OSS provides three storage classes: Standard, Infrequent Access, and Archive.
  128. // Upload a strings, and you can append some strings in the behind of object. but the object is 'Archive' storange class.
  129. // An object created with the AppendObject operation is an appendable object. set the object storange-class to 'Archive'.
  130. nextPos, err = bucket.AppendObject(appendObjectKey, strings.NewReader("昨夜雨疏风骤,浓睡不消残酒。试问卷帘人,"), nextPos, oss.ObjectStorageClass("Archive"))
  131. if err != nil {
  132. HandleError(err)
  133. }
  134. // Delete the object and bucket
  135. err = DeleteTestBucketAndObject(bucketName)
  136. if err != nil {
  137. HandleError(err)
  138. }
  139. fmt.Println("AppendObjectSample completed")
  140. }