Copy files (Harmony SDK)

Updated at:

Copy objects in a versioning-enabled bucket within the same region using the Harmony SDK.

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

Copy a small object

Call CopyObject to copy an object up to 1 GB from a source bucket to a destination bucket in 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, OSS returns HTTP 404. Use copySourceVersionId to copy a specific version. Delete markers cannot be copied.

  • Copy a previous version of an object to the same bucket to restore it as the current version.

  • If versioning is enabled for the destination bucket, OSS generates a unique version ID for the new object, returned in the x-oss-version-id response header. If versioning is disabled or suspended, OSS generates a version with a version ID of "null", which overwrites any existing "null" version.

  • 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

  • API reference for small object copy: CopyObject.