Use CopyObject to copy an object smaller than 5 GiB from a source bucket to a destination bucket in the same region. The destination can be the same bucket or a different one.
Prerequisites
Before you begin, ensure that you have:
Read permissions on the source object, and read and write permissions on the destination bucket
No retention policies configured on either the source or destination bucket (if a retention policy exists, the operation fails with:
The object you specified is immutable.)Both buckets in the same region (for example, objects in China (Hangzhou) cannot be copied to a bucket in China (Qingdao))
Note: The sample code in this topic uses the China (Hangzhou) region (cn-hangzhou) and retrieves access credentials from environment variables. By default, the public endpoint is used to access resources in a bucket. If you want to access resources in the bucket from other Alibaba Cloud services in the same region as the bucket, use the internal endpoint. For details, see Configure access credentials and Regions and endpoints.Permissions
By default, an Alibaba Cloud account has full permissions. RAM users and RAM roles have no permissions by default — grant them access via RAM Policy or Bucket Policy.
| Action | Required when |
|---|---|
oss:GetObject | Always |
oss:PutObject | Always |
oss:GetObjectVersion | Specifying a source object version via versionId |
oss:GetObjectTagging | Copying object tags via x-oss-tagging |
oss:PutObjectTagging | Copying object tags via x-oss-tagging |
oss:GetObjectVersionTagging | Specifying tags for a specific version via versionId |
kms:GenerateDataKey | Destination object uses X-Oss-Server-Side-Encryption: KMS |
kms:Decrypt | Destination object uses X-Oss-Server-Side-Encryption: KMS |
Method signature
func (c *Client) CopyObject(ctx context.Context, request *CopyObjectRequest, optFns ...func(*Options)) (*CopyObjectResult, error)Request parameters
| Parameter | Type | Description |
|---|---|---|
ctx | context.Context | Request context, used to set the total request duration |
request | *CopyObjectRequest | Request parameters. See CopyObjectRequest |
optFns | ...func(*Options) | Optional client-level settings. See Options |
CopyObjectRequest fields
| Field | Type | Description |
|---|---|---|
Bucket | *string | Destination bucket name |
Key | *string | Destination object name |
SourceBucket | *string | Source bucket name |
SourceKey | *string | Source object name |
ForbidOverwrite | *string | Whether to overwrite an existing object with the same name |
Tagging | *string | Tags for the destination object. Format: TagA=A&TagB=B |
TaggingDirective | *string | How to set tags on the destination object. Copy (default): copy tags from the source object. Replace: use the tags specified in the request |
Response parameters
| Parameter | Type | Description |
|---|---|---|
result | *CopyObjectResult | Response when err is nil. See CopyObjectResult |
err | error | Non-nil if the request fails |
Copy an object
The following example copies an object from a source bucket to a destination bucket. All parameters are passed as command-line flags.
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")
}
// If no destination bucket is specified, copy within the same bucket.
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 credentials from environment variables and set the region.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
client := oss.NewClient(cfg)
request := &oss.CopyObjectRequest{
Bucket: oss.Ptr(destBucketName), // Destination bucket
Key: oss.Ptr(destObjectName), // Destination object name
SourceKey: oss.Ptr(srcObjectName), // Source object name
SourceBucket: oss.Ptr(srcBucketName), // Source bucket
}
result, err := client.CopyObject(context.TODO(), request)
if err != nil {
log.Fatalf("failed to copy object: %v", err)
}
log.Printf("copy object result: %#v\n", result)
}What's next
For the complete sample code, see GitHub example.
For the full API reference, see CopyObject.