Skip to content

Instantly share code, notes, and snippets.

@shal
Created June 26, 2020 12:11
Show Gist options
  • Save shal/6f691a52cb1ec4a93679e61addd883d3 to your computer and use it in GitHub Desktop.
Save shal/6f691a52cb1ec4a93679e61addd883d3 to your computer and use it in GitHub Desktop.
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)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment