Copy an object (Go SDK V2)
Use CopyObject to copy an object smaller than 1 GB within a versioning-enabled bucket. For objects larger than 1 GB, use UploadPartCopy to perform a multipart copy.
Prerequisites
Before you begin, ensure that you have:
An OSS bucket with versioning enabled
The
oss:GetObjectandoss:PutObjectpermissions. For details, see Grant custom permissions to a RAM userAccess credentials configured in environment variables. For details, see Configure access credentials
Usage notes
The sample code uses the China (Hangzhou) region (
cn-hangzhou) and a public endpoint. To access OSS from another Alibaba Cloud service in the same region, use an internal endpoint instead. For supported regions and endpoints, see OSS regions and endpoints.Appendable objects cannot be copied to a bucket with versioning enabled or suspended.
Copy an object
CopyObject copies objects up to 1 GB from a source bucket to a destination bucket within the same region. By default, it copies the current version of the source object. To copy a specific version, set SourceVersionId in the request.
Versioning behavior:
| Scenario | Behavior |
|---|---|
Current version is a delete marker, no SourceVersionId specified | OSS returns 404 Not Found, which indicates that the object does not exist |
SourceVersionId points to a delete marker | Delete markers cannot be copied |
| Versioning enabled on destination bucket | OSS generates a unique version ID for the copied object and returns it in the x-oss-version-id response header |
| Versioning disabled or suspended on destination bucket | OSS assigns a null version ID; this overwrites any existing null-versioned object |
| Restoring a previous version | Copy it to the same bucket; the copied object becomes the new current version |
The following example copies a specific version of an object and sets a new storage class and tags on the destination object.
package main
import (
"context"
"flag"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
var (
region string
srcBucketName string
srcObjectName string
destBucketName string
destObjectName string
)
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&srcBucketName, "src-bucket", "", "The name of the source bucket.")
flag.StringVar(&srcObjectName, "src-object", "", "The name of the source object.")
flag.StringVar(&destBucketName, "dest-bucket", "", "The name of the destination bucket.")
flag.StringVar(&destObjectName, "dest-object", "", "The name of the destination object.")
}
func main() {
flag.Parse()
if len(srcBucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, source bucket name required")
}
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
// Default to the source bucket if no destination bucket is specified.
if len(destBucketName) == 0 {
destBucketName = srcBucketName
}
if len(srcObjectName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, source object name required")
}
if len(destObjectName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, destination object name required")
}
// Load default config with credentials from environment variables.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
client := oss.NewClient(cfg)
// Copy a specific version of the source object to the destination.
// StorageClass and Tagging override the source object's values.
copyRequest := &oss.CopyObjectRequest{
Bucket: oss.Ptr(destBucketName),
Key: oss.Ptr(destObjectName),
SourceKey: oss.Ptr(srcObjectName),
SourceBucket: oss.Ptr(srcBucketName),
SourceVersionId: oss.Ptr("yourVersionId"), // Replace with the actual version ID.
StorageClass: oss.StorageClassStandard,
TaggingDirective: oss.Ptr("Replace"), // Do not inherit tags from the source object.
Tagging: oss.Ptr("tag1=value1&tag2=value2"), // Tags to set on the destination object.
}
copyResult, err := client.CopyObject(context.TODO(), copyRequest)
if err != nil {
log.Fatalf("failed to copy object: %v", err)
}
// VersionId is the version ID assigned to the newly created object in the destination bucket.
log.Printf("copy object result versionId: %#v\n", *copyResult.VersionId)
}