All Products
Search
Document Center

Object Storage Service:Copy an object (Harmony SDK)

Last Updated:Mar 20, 2026

Use the copyObject method 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 bucket.

Prerequisites

Before you begin, ensure that you have:

  • Read permissions on the source object

  • Read and write permissions on the destination bucket

  • No retention policies configured on either the source or destination bucket (a retention policy causes the operation to fail with: The object you specified is immutable.)

For supported regions and endpoints, see OSS regions and endpoints.

Permissions

By default, an Alibaba Cloud account has full permissions. RAM users and RAM roles have no permissions by default and must be granted access through a 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 of a specific version via versionId
kms:GenerateDataKeyDestination metadata contains X-Oss-Server-Side-Encryption: KMS
kms:DecryptDestination metadata contains X-Oss-Server-Side-Encryption: KMS

Copy an object

Both scenarios use client.copyObject() with the same client initialization. The only difference is whether you include copySourceBucket.

Copy within the same bucket

Omit copySourceBucket when the source and destination are in the same bucket.

import Client, { RequestError } from '@aliyun/oss';

const client = new Client({
  // AccessKey ID from Security Token Service (STS).
  accessKeyId: 'yourAccessKeyId',
  // AccessKey secret from STS.
  accessKeySecret: 'yourAccessKeySecret',
  // Security token from STS.
  securityToken: 'yourSecurityToken',
  // Region where the bucket is located, for example: oss-cn-hangzhou.
  region: 'oss-cn-hangzhou',
});

const copyObject = async () => {
  try {
    const res = await client.copyObject({
      bucket: 'targetBucket',         // Destination bucket.
      key: 'targetKey',               // Destination object key.
      copySourceKey: 'sourceKey',     // Source object key.
    });

    console.log(JSON.stringify(res));
  } catch (err) {
    if (err instanceof RequestError) {
      // Handle known OSS errors.
      console.log('code: ', err.code);           // Error code.
      console.log('message: ', err.message);     // Error message.
      console.log('requestId: ', err.requestId); // Request ID for troubleshooting.
      console.log('status: ', err.status);       // HTTP status code.
      console.log('ec: ', err.ec);               // Error category.
    } else {
      console.log('unknown error: ', err);
    }
  }
};

copyObject();

Copy from a different bucket in the same region

Add copySourceBucket to specify the source bucket. Both buckets must be in the same region — cross-region copy is not supported.

import Client, { RequestError } from '@aliyun/oss';

const client = new Client({
  // AccessKey ID from STS.
  accessKeyId: 'yourAccessKeyId',
  // AccessKey secret from STS.
  accessKeySecret: 'yourAccessKeySecret',
  // Security token from STS.
  securityToken: 'yourSecurityToken',
  // Region where both buckets are located, for example: oss-cn-hangzhou.
  region: 'oss-cn-hangzhou',
});

const copyObjectFromOtherBucket = async () => {
  try {
    const res = await client.copyObject({
      bucket: 'targetBucket',               // Destination bucket.
      key: 'targetKey',                     // Destination object key.
      copySourceBucket: 'sourceBucket',     // Source bucket (required for cross-bucket copy).
      copySourceKey: 'sourceKey',           // Source object key.
    });

    console.log(JSON.stringify(res));
  } catch (err) {
    if (err instanceof RequestError) {
      // Handle known OSS errors.
      console.log('code: ', err.code);
      console.log('message: ', err.message);
      console.log('requestId: ', err.requestId);
      console.log('status: ', err.status);
      console.log('ec: ', err.ec);
    } else {
      console.log('unknown error: ', err);
    }
  }
};

copyObjectFromOtherBucket();

Usage notes

  • Size limit: copyObject supports objects smaller than 5 GiB.

  • Same region only: Source and destination buckets must be in the same region. For example, copying from China (Hangzhou) to China (Qingdao) is not supported.

  • Retention policies: If either bucket has a retention policy, the copy operation fails with The object you specified is immutable.