All Products
Search
Document Center

Object Storage Service:Copy a file (Harmony SDK)

Last Updated:Nov 29, 2025

This topic describes how to copy an object in a versioning-enabled bucket.

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 policies.

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

Copy a small object

You can call CopyObject to copy an object up to 1 GB in size from a source bucket to a destination bucket within the same region.

A target bucket.

  • By default, x-oss-copy-source copies the current version of the object. If the current version is a delete marker, Object Storage Service (OSS) returns the 404 HTTP status code, which indicates that the object does not exist. You can use copySourceVersionId to copy a specific version of the object. Delete markers cannot be copied.

  • You can copy a previous version of an object to the same bucket. The copied version then becomes the current version of the object, which restores the previous version.

  • If versioning is enabled for the destination bucket, OSS generates a unique version ID for the new object. This version ID is returned in the x-oss-version-id header of the response. If versioning is not enabled or is suspended for the destination bucket, OSS generates a version with a version ID of "null". This new version overwrites the original version that has a version ID of "null".

  • You can copy appendable objects only if versioning is not enabled for the destination bucket.

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

// Create an OSS client.
const client = new Client({
  // Replace with the AccessKey ID of the STS temporary credential.
  accessKeyId: 'yourAccessKeyId',
  // Replace with the AccessKey secret of the STS temporary credential.
  accessKeySecret: 'yourAccessKeySecret',
  // Replace with the security token of the STS temporary credential.
  securityToken: 'yourSecurityToken',
  // Specify the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to oss-cn-hangzhou.
  region: 'oss-cn-hangzhou',
});

// Copy an object based on its version ID.
const copyObjectByVersionId = async () => {
  try {
    // Call the copyObject method to copy an object by its specified version ID.
    const res = await client.copyObject({
      bucket: 'targetBucket', // The name of the destination bucket.
      key: 'targetKey', // The name of the destination object.
      copySourceKey: 'sourceKey', // The name of the source object.
      copySourceVersionId: 'sourceVersionId', // The version ID of the source object.
      // copySourceBucket:'sourceBucket', // Optional. The name of the source bucket. If you do not configure this parameter, its value defaults to the value of the bucket parameter.
    });

    // Print the result, which includes the response of the copy operation.
    console.log(JSON.stringify(res));
  } catch (err) {
    // Catch and handle request errors.
    if (err instanceof RequestError) {
      console.log('Error code: ', err.code); // The error code. For example, NoSuchKey indicates that the object does not exist.
      console.log('Error message: ', err.message); // The error message, which provides more details.
      console.log('Request ID: ', err.requestId); // The unique ID of the request, which is used for troubleshooting.
      console.log('HTTP status code: ', err.status); // The HTTP status code. For example, 404 indicates that the resource is not found.
      console.log('Error category: ', err.ec); // The error category, which further classifies the error type.
    } else {
      console.log('Unknown error: ', err); // An error that is not of the RequestError type, possibly another exception.
    }
  }
};

// Call the function to copy the object based on its version ID.
copyObjectByVersionId();

References

  • For more information about copying small objects, see CopyObject.