package main
import (
"log"
"time"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
log.Fatalf("Failed to create credentials provider: %v", err)
}
// Create an OSSClient instance.
// Set yourEndpoint to the endpoint of the bucket. For example, for the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
// Set yourRegion to the region where the bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature version.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
log.Fatalf("Failed to create OSS client: %v", err)
}
// Specify the bucket name, for example, examplebucket.
bucketName := "examplebucket"
// Specify the full path of the source object, for example, srcdir/srcobject.jpg.
objectName := "srcdir/srcobject.jpg"
// Specify the full path of the destination object, for example, destdir/destobject.jpg.
destObjectName := "destdir/destobject.jpg"
// Get the bucket.
bucket, err := client.Bucket(bucketName)
if err != nil {
log.Fatalf("Failed to get bucket '%s': %v", bucketName, err)
}
// Specify the metadata of the destination object.
expires := time.Date(2049, time.January, 10, 23, 0, 0, 0, time.UTC)
tag1 := oss.Tag{
Key: "a",
Value: "1",
}
taggingInfo := oss.Tagging{
Tags: []oss.Tag{tag1},
}
options := []oss.Option{
oss.MetadataDirective(oss.MetaReplace),
oss.Expires(expires),
oss.SetTagging(taggingInfo),
// Copy the object tagging of the source object to the destination object.
// oss.TaggingDirective(oss.TaggingCopy),
// Specify the access permissions (ACL) of the destination object as private when the destination object is created.
// oss.ObjectACL(oss.ACLPrivate),
// Specify the customer master key (CMK) managed by KMS. This parameter is valid only when x-oss-server-side-encryption is set to KMS.
// oss.ServerSideEncryptionKeyID("9468da86-3509-4f8d-a61e-6eab1eac****"),
// Specify the server-side encryption algorithm that OSS uses to create the destination object.
// oss.ServerSideEncryption("AES256"),
// Copy the metadata of the source object to the destination object.
// oss.MetadataDirective(oss.MetaCopy),
// Specify whether the CopyObject operation overwrites a destination object that has the same name. In this example, the value is set to true, which prevents overwriting an object that has the same name.
// oss.ForbidOverWrite(true),
// If the ETag of the source object matches the ETag you provide, the copy operation is performed and 200 OK is returned.
// oss.CopySourceIfMatch("5B3C1A2E053D763E1B002CC607C5****"),
// If the ETag of the source object does not match the ETag you provide, the copy operation is performed and 200 OK is returned.
// oss.CopySourceIfNoneMatch("5B3C1A2E053D763E1B002CC607C5****"),
// If the specified time is earlier than the actual modification time of the object, the object is copied and 200 OK is returned.
// oss.CopySourceIfModifiedSince(time.Date(2021, time.December, 9, 7, 1, 56, 0, time.UTC)),
// If the specified time is the same as or later than the actual modification time of the object, the object is copied and 200 OK is returned.
// oss.CopySourceIfUnmodifiedSince(time.Date(2021, time.December, 9, 7, 1, 56, 0, time.UTC)),
// Specify the storage class of the object. In this example, the value is set to Standard, which specifies the Standard storage class.
// oss.StorageClass("Standard"),
}
// Use the specified metadata to overwrite the metadata of the source object.
_, err = bucket.CopyObject(objectName, destObjectName, options...)
if err != nil {
log.Fatalf("Failed to copy object from '%s' to '%s': %v", objectName, destObjectName, err)
}
log.Printf("Successfully copied object from '%s' to '%s'", objectName, destObjectName)
}