6 "github.com/aliyun/aliyun-oss-go-sdk/oss"
9 // CopyObjectSample shows the copy files usage
10 func CopyObjectSample() {
12 bucket, err := GetTestBucket(bucketName)
18 err = bucket.PutObjectFromFile(objectKey, localFile)
23 // Case 1: Copy an existing object
24 var descObjectKey = "descobject"
25 _, err = bucket.CopyObject(objectKey, descObjectKey)
30 // Case 2: Copy an existing object to another existing object
31 _, err = bucket.CopyObject(objectKey, descObjectKey)
36 err = bucket.DeleteObject(descObjectKey)
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))
47 fmt.Println("CopyObjectError:", err)
48 // Constraints are met, the copy executes
49 _, err = bucket.CopyObject(objectKey, descObjectKey, oss.CopySourceIfUnmodifiedSince(futureDate))
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...)
64 meta, err := bucket.GetObjectDetailedMeta(descObjectKey)
68 fmt.Println("meta:", meta)
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)}
76 _, err = bucket.CopyObject(objectKey, objectKey, options...)
80 fmt.Println("meta:", meta)
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)
89 // Part size is 100K and three coroutines for the concurrent copy
90 err = bucket.CopyFile(bucketName, objectKey, descObjectKey, 100*1024, oss.Routines(3))
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, ""))
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"))
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"))
114 // Case 8: Copy object with tagging, the value of tagging directive is REPLACE
123 tagging := oss.Tagging{
124 Tags: []oss.Tag{tag1, tag2},
126 _, err = bucket.CopyObject(objectKey, objectKey+"WithTagging", oss.SetTagging(tagging), oss.TaggingDirective(oss.TaggingReplace))
131 // Delete object and bucket
132 err = DeleteTestBucketAndObject(bucketName)
137 fmt.Println("CopyObjectSample completed")