All Products
Search
Document Center

Object Storage Service:Copy objects (OSS SDK for Go V2)

Last Updated:Mar 20, 2026

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.

ActionRequired when
oss:GetObjectAlways
oss:PutObjectAlways
oss:GetObjectVersionSpecifying a source object version via versionId
oss:GetObjectTaggingCopying object tags via x-oss-tagging
oss:PutObjectTaggingCopying object tags via x-oss-tagging
oss:GetObjectVersionTaggingSpecifying tags for a specific version via versionId
kms:GenerateDataKeyDestination object uses X-Oss-Server-Side-Encryption: KMS
kms:DecryptDestination 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

ParameterTypeDescription
ctxcontext.ContextRequest context, used to set the total request duration
request*CopyObjectRequestRequest parameters. See CopyObjectRequest
optFns...func(*Options)Optional client-level settings. See Options

CopyObjectRequest fields

FieldTypeDescription
Bucket*stringDestination bucket name
Key*stringDestination object name
SourceBucket*stringSource bucket name
SourceKey*stringSource object name
ForbidOverwrite*stringWhether to overwrite an existing object with the same name
Tagging*stringTags for the destination object. Format: TagA=A&TagB=B
TaggingDirective*stringHow 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

ParameterTypeDescription
result*CopyObjectResultResponse when err is nil. See CopyObjectResult
errerrorNon-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(&region, "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