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