This topic describes how to copy objects in a bucket with versioning enabled or suspended.

Copy small objects

You can use the CopyObject method to copy an object smaller than 1 GB from a bucket (source bucket) to another bucket (target bucket) in the same region.

Note
  • CopyObject copies the current version of an object by default. If the current version of the target object is a delete marker, the 404 Not Found error is returned to indicate that the object does not exist. You can specify the versionId in a CopyObject request to copy a specified version of the target object. However, a delete marker cannot be copied
  • You can copy a historical version of an object to the bucket that stores the object. The copied historical version becomes the current version of a new object. You can use this method to restore a historical version of an object.
  • If versioning is enabled for the bucket that stores the target object, OSS generates a unique version ID for the copied object, which is returned in the x-oss-version-id field in the response header. If versioning is suspended or not enabled for the bucket that stores the target object, OSS generates a version of which the ID is null for the copied object and overwrite overwrites the original version of which the ID is null.
  • If versioning is enabled or suspended for a bucket, you cannot perform the CopyObject operation on appendable objects in the bucket.

The following code provides an example on how to copy objects smaller than 1 GB:

const OSS = require('ali-oss');

const client = new OSS({
  bucket: '<Your BucketName>',
  // The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint.
  region: '<Your Region>',
  // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
  accessKeyId: '<Your AccessKeyId>',
  accessKeySecret: '<Your AccessKeySecret>',
});

// Specify the version ID of the object you want to copy.
const versionId = 'your versionId';
async function copy() {
  const result = await client.copy(target, origin, { versionId });
  console.log(result.res.headers);
}
copy();

For more information about copying objects smaller than 1 GB, see CopyObject.

Copy large objects

To copy an object larger than 1 GB, you must use the multipartUploadCopy method.

By default, multipartUploadCopy uploads a part by copying the data from the current version of an existing object. You can add versionId in the request header x-oss-copy-source as the condition to upload a part by copying the data from a specified version of an existing object. Example: x-oss-copy-source : /SourceBucketName/SourceObjectName? versionId=111111.

Note The SourceObjectName parameter, which is a URL, must be encoded. The version ID of the copied object is returned as the x-oss-copy-source-version-id field in the response.

If you do not specify the versionId in the request header and the current version of the object to be copied is a delete marker, the 404 Not Found error is returned. If you specify the versionId in the request header and the specified version of the object is a delete marker, the 400 Bad Request error is returned.

The following code provides an example for multipart copy:

const OSS = require('ali-oss');

const client = new OSS({
  bucket: '<Your BucketName>',
  // The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint.
  region: '<Your Region>',
  // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
  accessKeyId: '<Your AccessKeyId>',
  accessKeySecret: '<Your AccessKeySecret>'
});

// Specify the version ID of the object you want to copy.
versionId = 'your versionId';

async function multipartUploadCopy() {
  const result = await client.multipartUploadCopy(
    copyName,
    {
      sourceKey: name,
      sourceBucketName: bucket,
    },
    {
      versionId
    }
  );
  console.log(result);
}
multipartUploadCopy();

For more information about UploadPartCopy, see UploadPartCopy.