This topic describes how to copy an object in a versioned bucket. You can use CopyObject to copy an object that is up to 1 GB in size and use UploadPartCopy to copy an object 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.
By default, the x-oss-copy-source header specifies the current version of the object to copy. If the current version of the object is a delete marker, OSS returns HTTP status code 404. The HTTP status code indicates that the object does not exist. You can add a version ID to the x-oss-copy-source header to copy a specified 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 version of the object. This way, the previous version of the object is recovered.
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 response as the x-oss-version-id header value. 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 original 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({
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou.
region: 'yourregion',
// Obtain access credentials from environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
// Specify the name of the bucket.
bucket: 'yourbucketname'
});
// Specify the version ID of the source object.
const versionId = 'versionId';
// Specify the source object to copy.
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
To copy an object larger than 1 GB, you must split the object into parts and copy the parts sequentially by using multipartUploadCopy.
By default, multipartUploadCopy uploads a part by copying data from the current version of an existing object. You can add versionId to the x-oss-copy-source request header as a condition to upload a part by copying data from the specified version of an existing object. Example: x-oss-copy-source: /SourceBucketName/SourceObjectName?versionId=111111.
The value of SourceObjectName must be URL-encoded. The version ID of the copied object is returned as the value of the x-oss-copy-source-version-id header in the response.
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({
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou.
region: 'yourregion',
// Obtain access credentials from environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
// Specify the name of the bucket.
bucket: 'yourbucketname'
});
// Specify the version ID of the source object.
const versionId = 'versionId';
// Specify the source object to copy.
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 that you can call to copy a small object, see CopyObject.
For more information about the API operation that you can call to copy a large object, see UploadPartCopy.