All Products
Search
Document Center

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

Last Updated:Aug 05, 2025

This topic describes how to use the CopyObject method of the OSS SDK for Go V2 to copy an object smaller than 5 GiB from a source bucket to a destination bucket in the same region. The destination bucket can be the source bucket or a different bucket.

Usage notes

  • The sample code in this topic uses the China (Hangzhou) region ID, cn-hangzhou. 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 more information about OSS regions and endpoints, see Regions and endpoints.

  • In this topic, access credentials are retrieved from environment variables. For more information about how to configure access credentials, see Configure access credentials.

  • To copy an object, you must have read permissions on the source object and read and write permissions on the destination bucket.

  • The source bucket and destination bucket must be located in the same region. For example, objects in a bucket located in the China (Hangzhou) region cannot be copied to another bucket located in the China (Qingdao) region.

  • Make sure that no retention policies are configured for the source bucket and the destination bucket. Otherwise, the following error message is returned: 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 Policy or Bucket Policy.

API

Action

Definition

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

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

Request parameters

Parameter

Type

Description

ctx

context.Context

The context of the request, which can be used to specify the total duration of the request.

request

*CopyObjectRequest

Specifies the parameters of a specific API operation. For more information, see CopyObjectRequest.

optFns

...func(*Options)

Optional parameters. For more information, see Options.

Common parameters of CopyObjectRequest

Parameter

Type

Description

Bucket

*string

The name of the destination bucket.

Key

*string

The name of the destination object.

SourceBucket

*string

The name of the source bucket.

SourceKey

*string

The name of the source object.

ForbidOverwrite

*string

Specifies whether the CopyObject operation overwrites an existing object that has the same name.

Tagging

*string

The tags of the destination object. You can configure multiple tags for the destination object. Example: TagA=A&TagB=B.

TaggingDirective

*string

The method that is used to configure tags for the destination object. Valid values:

  • Copy (default): The tags of the source object are copied to the destination object.

  • Replace: The tags specified in the request are configured for the destination object.

Response parameters

Response parameter

Type

Description

result

*CopyObjectResult

The response to the operation. This parameter is valid when the value of err is nil. For more information, see CopyObjectResult.

err

error

The status of the request. If the request fails, the value of err cannot be nil.

Example

The following sample code provides an example of how 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.
	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 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, the source bucket name is used.
	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 configurations 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 to copy an 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.
	}

	// Copy the source object and process the results.
	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 to copy an object, see GitHub Example.

  • For more information about the API operation for copying an object, see CopyObject.