Created
June 26, 2020 12:11
-
-
Save shal/6f691a52cb1ec4a93679e61addd883d3 to your computer and use it in GitHub Desktop.
Copy objects to another bucket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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