This topic describes how to copy an object in a versioning-enabled bucket. You can call CopyObject to copy an object that is up to 1 GB in size and call UploadPartCopy to copy an object that is larger than 1 GB in size.
Copy a small object
You can call CopyObject to copy an object that is up to 1 GB in size from a source bucket to a destination bucket within the same region.
If you do not specify a version ID in the x-oss-copy-source header of the CopyObject request, the current version of the object is copied. If the current version of the object is a delete marker, OSS returns 404, which indicates that the object does not exist. You can add a version ID to the x-oss-copy-source header to copy a specific object version. Delete markers cannot be copied.
You can copy a previous version of an object to the same bucket. The copied previous version becomes the new current version of the object. This way, the previous version of the object is restored.
If versioning is enabled for the destination bucket, OSS generates a unique version ID for the destination object. The version ID is returned in the x-oss-version-id response header. If versioning is disabled or suspended for the destination bucket, OSS generates a version whose version ID is null for the destination object and overwrites the existing version whose version ID is null.
Appendable objects cannot be copied to a destination bucket for which versioning is enabled or suspended.
The following sample code provides an example on how to copy a small object:
const OSS = require('ali-oss');
const client = new OSS({
// Set yourregion to 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: 'yourregion',
// Obtain access credentials from environment variables. Before running this code sample, make sure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Set yourbucketname to the name of the bucket.
bucket: 'yourbucketname'
});
// Specify the versionId of the source object.
const versionId = 'versionId';
// Specify the source object.
const srcObject = 'srcObject.txt';
// Specify the source bucket.
const srcBucket = 'srcBucket';
// Specify the destination object.
const targetObject = 'targetObject.txt';
async function Copy() {
try {
const result = await client.copy(targetObject, srcObject, srcBucket, {
meta: {
versionId: versionId
}
});
console.log(result);
} catch (error) {
console.error(error);
}
}
Copy()Copy a large object
For objects larger than 1 GB, use the multipart copy (UploadPartCopy) method.
By default, a multipart copy operation copies data from the current version of an existing object to upload a part. To copy from a specific version of an object, include the versionId in the x-oss-copy-source request header. For example: x-oss-copy-source: /SourceBucketName/SourceObjectName?versionId=111111.
The SourceObjectName must be URL-encoded. The response returns the version ID of the copied object in the x-oss-copy-source-version-id header.
If a version ID is not specified in the request and the current version of the source object is a delete marker, OSS returns 404 Not Found. If a version ID is specified in the request and the specified version of the source object is a delete marker, OSS returns 400 Bad Request.
The following sample code provides an example on how to copy a large object by using multipart copy:
const OSS = require('ali-oss');
const client = new OSS({
// Set yourregion to 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: 'yourregion',
// Obtain access credentials from environment variables. Before running this code sample, make sure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Set yourbucketname to the name of the bucket.
bucket: 'yourbucketname'
});
// Specify the versionId of the source object.
const versionId = 'versionId';
// Specify the source object.
const srcObject = "srcObject.txt";
// Specify the source bucket.
const srcBucket = 'srcBucket';
// Specify the destination object.
const targetObject = "targetObject.txt";
async function multipartUploadCopy() {
const result = await client.multipartUploadCopy(
targetObject,
{
sourceKey: srcObject,
sourceBucketName: srcBucket,
},
{
versionId
}
);
console.log(result);
}
multipartUploadCopy();References
For more information about the API operation for copying small objects, see CopyObject.
For more information about the API operation for copying large objects, see UploadPartCopy.