All Products
Search
Document Center

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

Last Updated:Aug 25, 2026

Use the CopyObject method of SDK for Go V2 to copy an object smaller than 5 GiB between a source bucket and a destination bucket in the same region. The source and destination buckets can be the same or different.

Precautions

  • The sample code in this topic uses the China (Hangzhou) region ID cn-hangzhou and a public endpoint by default. If you want to access OSS from another Alibaba Cloud service in the same region, use an internal endpoint instead. For the mapping between OSS regions and endpoints, see Regions and endpoints.

  • This topic uses environment variables as an example to read access credentials. For information about how to configure access credentials, see Configure access credentials.

  • Read permission on the source object and read and write permissions on the destination bucket are required to copy an object.

  • Cross-region copy is not supported. For example, you cannot copy an object from a bucket in the China (Hangzhou) region to a bucket in the China (Qingdao) region.

  • When you copy an object, make sure that neither the source bucket nor the destination bucket has a retention policy configured. Otherwise, the operation fails and returns the error: The object you specified is immutable.

Permissions

By default, an Alibaba Cloud account has full permissions. RAM users or RAM roles under an Alibaba Cloud account do not have any permissions by default. The Alibaba Cloud account or account administrator must grant operation permissions through RAM policies or Bucket Policy.

API

Action

Description

CopyObject

oss:GetObject

Copies objects within a bucket or between buckets in the same region.

oss:PutObject

oss:GetObjectVersion

If you specify the source object version through versionId, this permission is also required.

oss:GetObjectTagging

If you copy object tags through x-oss-tagging, these permissions are required.

oss:PutObjectTagging

oss:GetObjectVersionTagging

If you specify the tags of a specific version of the source object through versionId, this permission is also required.

kms:GenerateDataKey

When copying an object, if the destination object metadata contains X-Oss-Server-Side-Encryption: KMS, these two permissions are required.

kms:Decrypt

Method signature

func (c *Client) CopyObject(ctx context.Context, request *CopyObjectRequest, optFns ...func(*Options)) (*CopyObjectResult, error)

Request parameters

Parameter

Type

Description

ctx

context.Context

The request context. You can use it to set the overall timeout for the request.

request

*CopyObjectRequest

The request parameters for this operation. For more information, see CopyObjectRequest

optFns

...func(*Options)

(Optional) The configuration options. For more information, see Options

The following table describes the common parameters of CopyObjectRequest:

Parameter

Type

Description

Bucket

*string

Specifies the name of the destination bucket.

Key

*string

Specifies the name of the destination object.

SourceBucket

*string

Specifies the name of the source bucket.

SourceKey

*string

Specifies the name of the source object.

ForbidOverwrite

*string

Specifies whether to overwrite the destination object that has the same name during the CopyObject operation.

Tagging

*string

Specifies the tags of the object. You can specify multiple tags at the same time, for example, TagA=A&TagB=B.

TaggingDirective

*string

Specifies how to set the tags of the destination object. Valid values:

  • Copy (default): copies the tags of the source object to the destination object.

  • Replace: ignores the tags of the source object and uses the tags specified in the request.

Response parameters

Return value

Type

Description

result

*CopyObjectResult

The return value of the operation. This value takes effect when err is nil. For more information, see CopyObjectResult

err

error

The status of the request. If the request fails, err is not nil.

Sample code

Use the following sample code to copy an object smaller than 5 GiB from a source bucket to a destination bucket.

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"
)

// Specify the global variables.
var (
	region         string // The region in which the bucket is located.
	srcBucketName  string // The name of the source bucket.
	srcObjectName  string // The name of the source object.
	destBucketName string // The name of the destination bucket.
	destObjectName string // The name of the destination object.
)

// Specify the init function used to initialize command line parameters.
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() {
	// Parse the command line parameters.
	flag.Parse()

	// Check whether the source bucket name is empty.
	if len(srcBucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, source bucket name required")
	}

	// Check whether the region is empty.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// If the destination bucket name is not specified, use the source bucket name.
	if len(destBucketName) == 0 {
		destBucketName = srcBucketName
	}

	// Check whether the source object name is empty.
	if len(srcObjectName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, source object name required")
	}

	// Check whether the destination object name is empty.
	if len(destObjectName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, destination object name required")
	}

	// Load the default configuration and specify the credential provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	// Create an OSS client.
	client := oss.NewClient(cfg)

	// Create a request for copying the object.
	request := &oss.CopyObjectRequest{
		Bucket:       oss.Ptr(destBucketName), // The name of the destination bucket.
		Key:          oss.Ptr(destObjectName), // The name of the destination object.
		SourceKey:    oss.Ptr(srcObjectName),  // The name of the source object.
		SourceBucket: oss.Ptr(srcBucketName),  // The name of the source bucket.
	}

	// Perform the copy object operation and handle the result.
	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)
}

References

  • For the complete sample code for copying objects, see GitHub sample.

  • For the API operation used to copy objects, see CopyObject.