copy_object.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package sample
  2. import (
  3. "fmt"
  4. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  5. )
  6. // CopyObjectSample shows the copy files usage
  7. func CopyObjectSample() {
  8. // Create a bucket
  9. bucket, err := GetTestBucket(bucketName)
  10. if err != nil {
  11. HandleError(err)
  12. }
  13. // Create an object
  14. err = bucket.PutObjectFromFile(objectKey, localFile)
  15. if err != nil {
  16. HandleError(err)
  17. }
  18. // Case 1: Copy an existing object
  19. var descObjectKey = "descobject"
  20. _, err = bucket.CopyObject(objectKey, descObjectKey)
  21. if err != nil {
  22. HandleError(err)
  23. }
  24. // Case 2: Copy an existing object to another existing object
  25. _, err = bucket.CopyObject(objectKey, descObjectKey)
  26. if err != nil {
  27. HandleError(err)
  28. }
  29. err = bucket.DeleteObject(descObjectKey)
  30. if err != nil {
  31. HandleError(err)
  32. }
  33. // Case 3: Copy file with constraints. When the constraints are met, the copy executes. otherwise the copy does not execute.
  34. // constraints are not met, copy does not execute
  35. _, err = bucket.CopyObject(objectKey, descObjectKey, oss.CopySourceIfModifiedSince(futureDate))
  36. if err == nil {
  37. HandleError(err)
  38. }
  39. fmt.Println("CopyObjectError:", err)
  40. // Constraints are met, the copy executes
  41. _, err = bucket.CopyObject(objectKey, descObjectKey, oss.CopySourceIfUnmodifiedSince(futureDate))
  42. if err != nil {
  43. HandleError(err)
  44. }
  45. // Case 4: Specify the properties when copying. The MetadataDirective needs to be MetaReplace
  46. options := []oss.Option{
  47. oss.Expires(futureDate),
  48. oss.Meta("myprop", "mypropval"),
  49. oss.MetadataDirective(oss.MetaReplace)}
  50. _, err = bucket.CopyObject(objectKey, descObjectKey, options...)
  51. if err != nil {
  52. HandleError(err)
  53. }
  54. meta, err := bucket.GetObjectDetailedMeta(descObjectKey)
  55. if err != nil {
  56. HandleError(err)
  57. }
  58. fmt.Println("meta:", meta)
  59. // Case 5: When the source file is the same as the target file, the copy could be used to update metadata
  60. options = []oss.Option{
  61. oss.Expires(futureDate),
  62. oss.Meta("myprop", "mypropval"),
  63. oss.MetadataDirective(oss.MetaReplace)}
  64. _, err = bucket.CopyObject(objectKey, objectKey, options...)
  65. if err != nil {
  66. HandleError(err)
  67. }
  68. fmt.Println("meta:", meta)
  69. // Case 6: Big file's multipart copy. It supports concurrent copy with resumable upload
  70. // copy file with multipart. The part size is 100K. By default one routine is used without resumable upload
  71. err = bucket.CopyFile(bucketName, objectKey, descObjectKey, 100*1024)
  72. if err != nil {
  73. HandleError(err)
  74. }
  75. // Part size is 100K and three coroutines for the concurrent copy
  76. err = bucket.CopyFile(bucketName, objectKey, descObjectKey, 100*1024, oss.Routines(3))
  77. if err != nil {
  78. HandleError(err)
  79. }
  80. // Part size is 100K and three coroutines for the concurrent copy with resumable upload
  81. err = bucket.CopyFile(bucketName, objectKey, descObjectKey, 100*1024, oss.Routines(3), oss.Checkpoint(true, ""))
  82. if err != nil {
  83. HandleError(err)
  84. }
  85. // Specify the checkpoint file path. If the checkpoint file path is not specified, the current folder is used.
  86. err = bucket.CopyFile(bucketName, objectKey, descObjectKey, 100*1024, oss.Checkpoint(true, localFile+".cp"))
  87. if err != nil {
  88. HandleError(err)
  89. }
  90. // Case 7: Set the storage classes.OSS provides three storage classes: Standard, Infrequent Access, and Archive.
  91. // Copy a object in the same bucket, and set object's storage-class to Archive.
  92. _, err = bucket.CopyObject(objectKey, objectKey+"DestArchive", oss.ObjectStorageClass("Archive"))
  93. if err != nil {
  94. HandleError(err)
  95. }
  96. // Case 8: Copy object with tagging, the value of tagging directive is REPLACE
  97. tag1 := oss.Tag{
  98. Key: "key1",
  99. Value: "value1",
  100. }
  101. tag2 := oss.Tag{
  102. Key: "key2",
  103. Value: "value2",
  104. }
  105. tagging := oss.Tagging{
  106. Tags: []oss.Tag{tag1, tag2},
  107. }
  108. _, err = bucket.CopyObject(objectKey, objectKey+"WithTagging", oss.SetTagging(tagging), oss.TaggingDirective(oss.TaggingReplace))
  109. if err != nil {
  110. HandleError(err)
  111. }
  112. // Delete object and bucket
  113. err = DeleteTestBucketAndObject(bucketName)
  114. if err != nil {
  115. HandleError(err)
  116. }
  117. fmt.Println("CopyObjectSample completed")
  118. }