All Products
Search
Document Center

:Copy objects

Last Updated:Jan 03, 2025

This topic describes how to use the CopyObject method of OSS SDK for Go 2.0 to copy an object that is less than 5 GiB in size 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 region ID cn-hangzhou of the China (Hangzhou) region. By default, the public endpoint is used to access resources in a bucket. If you want to access resources in the bucket by using other Alibaba Cloud services in the same region in which the bucket is located, use the internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.

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

  • To copy an object, you must have the 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.

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.

Examples

The following sample code provides an example on how to copy an object that is less than 5 GiB in size 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 that is used to copy an object, visit GitHub.

  • For more information about the API operation that you can call to copy an object, see CopyObject.