For the complete code of copying objects, see GitHub.

Copy objects within the same bucket

Run the following code to copy objects within the same bucket:

package main

import (
	"fmt"
	"os"
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Create an OSSClient instance.
	client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
	if err ! = nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	bucketName := "<yourBucketName>"
	objectName := "<yourObjectName>"
	destObjectName := "<yourDestObjectName>"

	// Obtain the bucket.
	bucket, err := client.Bucket(bucketName)
	if err ! = nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Copy an object to another object in the same bucket.
	_, err = bucket.CopyObject(objectName, destObjectName)
	if err ! = nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
}

Copy objects to another bucket

You can copy an object from the current bucket to another bucket or from other buckets to the current bucket. Run the following code to copy objects to another bucket:

package main

import (
	"fmt"
	"os"
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Create an OSSClient instance.
	client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
	if err ! = nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	bucketName := "<yourBucketName>"
	srcBucketName := "<yourSrcBucketName>"
	dstBucketName := "<yourDstBucketName>"
	srcObjectName := "<yourSrcObjectName>"
	dstObjectName := "<yourDstObjectName>"

	// Obtain the bucket.
	bucket, err := client.Bucket(bucketName)
	if err ! = nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Copy the source object (srcObjectName) from other buckets (srcBucketName) to the current bucket.
	bucket.CopyObjectFrom(srcBucketName, srcObjectName, dstObjectName)
	if err ! = nil {
		fmt.Println("CopyObjectFrom Error:", err)
		os.Exit(-1)
	}

	// Copy the source object (srcObjectName) from the current bucket to another bucket (dstBucketName).
	bucket.CopyObjectTo(dstBucketName, dstObjectName, srcObjectName)
	if err ! = nil {
		fmt.Println("CopyObjectTo Error:", err)
		os.Exit(-1)
	}
}
  • Process object metadata during copy

    You can process the object metadata when copying the object by specifying the MetadataDirective parameter:

    • oss.MetaCopy: The default value, indicating that the metadata of the target object is the same with that of the source object, that is, copying the source object metadata.
    • oss.MetaReplace: Replace the source object metadata with the new metadata.
    Notice If you specify the MetadataDirective parameter to oss.MetaReplace, the metadata of the source object is overwritten and cannot be recovered. Therefore, perform this operation with caution.

    The following metadata can be specified if the value of MetadataDirective is oss.MetaReplace:

    Metadata Description
    CacheControl Specifies the cache action of the web page when the new object is downloaded.
    ContentDisposition Specifies the name of the new object when it is downloaded.
    ContentEncoding Specifies the content encoding format when a new object is downloaded.
    Expires Specify the expiration time (in GMT) of the new object.
    ServerSideEncryption Specifies the server-side encryption algorithm used when the OSS creates an object. Valid value: AES256
    ObjectACL Specifies ACL of an OSS object when it is created.
    Meta Custom parameter prefixed with X-Oss-Meta-.

    Run the following code to process object metadata during copy:

    package main
    
    import (
    	"fmt"
    	"os"
    	"time"
    	"github.com/aliyun/aliyun-oss-go-sdk/oss"
    )
    
    func main() {
    	// Create an OSSClient instance.
    	client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
    	if err ! = nil {
    		fmt.Println("Error:", err)
    		os.Exit(-1)
    	}
    
    	bucketName := "<yourBucketName>"
    	objectName := "<yourObjectName>"
    	destObjectName := "<yourDestObjectName>"
    
    	// Obtain the bucket.
    	bucket, err := client.Bucket(bucketName)
    	if err ! = nil {
    		fmt.Println("Error:", err)
    		os.Exit(-1)
    	}
    
    	// Specify the metadata of the target object.
    	expires := time.Date(2049, time.January, 10, 23, 0, 0, 0, time.UTC)
    	options := []oss.Option{
    		oss.MetadataDirective(oss.MetaReplace),
    		oss.Expires(expires),
    		oss.ObjectACL(oss.ACLPublicRead),
    		oss.Meta("MyMeta", "MyMetaValue")}
    
    	// Overwrites the metadata of the source object with the specified metadata.
    	_, err = bucket.CopyObject(objectName, destObjectName, options...)
    	if err ! = nil {
    		fmt.Println("Error:", err)
    		os.Exit(-1)
    	}
    }
    

Conditional copy

You can set conditions for object copy. The object is copied only when the conditions are satisfied. Otherwise, an error is reported and the object does not get copied. Available conditions include:

Parameter Description
CopySourceIfMatch If the source object’s ETag value is the same as the ETag provided by the user, the copy operation is executed. Otherwise, an error is returned.
CopySourceIfNoneMatch If the source object’s ETag value is not the same as the ETag provided by the user, the copy operation is actioned. Otherwise, an error is returned.
CopySourceIfModifiedSince If the specified time in the input parameter is the same as or later than the actual modification time of the source object, the object is copied normally. Otherwise, an error is returned.
CopySourceIfUnmodifiedSince If the source object has been modified after the time specified by the user, the object is copied. Otherwise, an error is returned.

CopySourceIfMatch and CopySourceIfNoneMatch can be used as conditions together. CopySourceIfModifiedSince and CopySourceIfUnmodifiedSince can be used as conditions together.

Run the following code for conditional copy:

package main

import (
	"fmt"
	"os"
	"time"
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Create an OSSClient instance.
	client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
	if err ! = nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// bucketName := "<yourBucketName>"
	// objectName := "<yourObjectName>"
	bucketName := "<yourBucketName>"
	objectName := "<yourObjectName>"
	destMatchObjectName := "<yourMatchDestObjectName>"
	destUnMatchObjectName := "<yourUnmatchDestObjectName>"

	// Obtain the bucket.
	bucket, err := client.Bucket(bucketName)
	if err ! = nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	date := time.Date(2011, time.November, 10, 23, 0, 0, 0, time.UTC)

	// The conditions are satisfied, copy the object.
	_, err = bucket.CopyObject(objectName, destMatchObjectName, oss.CopySourceIfModifiedSince(date))
	if err ! = nil {
		fmt.Println("CopyObject CopySourceIfModifiedSince Error:", err)
	}

	// The conditions are not met, the object is not copied.
	_, err = bucket.CopyObject(objectName, destUnMatchObjectName, oss.CopySourceIfUnmodifiedSince(date))
	if err ! = nil {
		fmt.Println("CopyObject CopySourceIfUnmodifiedSince Error:", err)
	}
}