This topic describes how to copy an object within a bucket or across buckets in the same region.
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions, endpoints and open ports.
In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Create an OSSClient instance.
To copy an object, you must have read permissions on the source object and read and write permissions on the destination bucket.
Make sure that no retention policies are configured for the source bucket and the destination bucket. Otherwise, the error message The object you specified is immutable. is returned.
The source bucket and destination bucket must be in the same region. For example, objects cannot be copied from a bucket located in the China (Hangzhou) region to another bucket located in the China (Qingdao) region.
Copy a small object
You can use the copy method to copy an object that does not exceed 1 GB in size within the same bucket or between different buckets in the same region.
Copy an object within the same bucket
The following sample code provides an example on how to copy an object within the same bucket:
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 the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. accessKeyId: process.env.OSS_ACCESS_KEY_ID, accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET, authorizationV4: true, // Specify the name of the bucket. Example: examplebucket. bucket: 'examplebucket', // Specify whether to enable HTTPS. If you set secure to true, HTTPS is enabled. // secure: true }) // Copy an object within a bucket. async function copySmallObjecInSameBucket() { try { // Specify the full paths of the source object and the destination object. Do not include the bucket name in the full paths. // Specify HTTP headers and metadata for the destination object. const result = await client.copy('destexampleobject.txt', 'srcexampleobject.txt', { // Configure the headers parameter to specify HTTP headers for the destination object. If you do not configure the headers parameter, the HTTP headers of the destination object are the same as the HTTP headers of the source object. The HTTP headers of the source object are copied. headers: { 'Cache-Control': 'no-cache', // If the ETag value of the source object is the same as the ETag value specified in the request, OSS copies the object and returns 200 OK. 'if-match': '5B3C1A2E053D763E1B002CC607C5****', // If the ETag value that you specify in the request is different from the ETag value of the source object, OSS copies the object and returns 200 OK. 'if-none-match': '5B3C1A2E053D763E1B002CC607C5****', // If the time that is specified in the request is earlier than the time when the object is modified, OSS copies the object and returns 200 OK. 'if-modified-since': '2021-12-09T07:01:56.000Z', // If the time that is specified in the request is later than the time when the object is modified, OSS copies the object and returns 200 OK. 'if-unmodified-since': '2021-12-09T07:01:56.000Z', // Specify the access control list (ACL) of the destination object. In this example, the ACL is set to private, which indicates that only the object owner and authorized users have read and write permissions on the object. 'x-oss-object-acl': 'private', // Specify tags for the object. You can specify multiple tags for the object at the same time. 'x-oss-tagging': 'Tag1=1&Tag2=2', // Specify whether the CopyObject operation overwrites an existing object that has the same name. In this example, this parameter is set to true, which specifies that the CopyObject operation does not overwrite an existing object that has the same name. 'x-oss-forbid-overwrite': 'true', }, // Configure the meta parameter to specify the metadata of the destination object. If you do not configure the meta parameter, the metadata of the destination object is the same as the metadata of the source object. The metadata of the source object is copied. meta: { location: 'hangzhou', year: 2015, people: 'mary', }, }); console.log(result); } catch (e) { console.log(e); } } copySmallObjecInSameBucket()
Copy an object across buckets in the same region
The following sample code provides an example on how to copy an object across buckets in the same region:
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 the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. accessKeyId: process.env.OSS_ACCESS_KEY_ID, accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET, authorizationV4: true, // Specify the name of the destination bucket. bucket: 'destexamplebucket', }); async function copySmallObjectBetweenBuckets() { try { // Specify the source object that you want to copy, the destination object to which you want to copy the source object, and the bucket in which the source object is stored. const result = await client.copy('destobject.txt', 'srcobject.txt', 'srcbucket', { // Configure the headers parameter to specify HTTP headers for the destination object. If you do not configure the headers parameter, the HTTP headers of the destination object are the same as the HTTP headers of the source object. The HTTP headers of the source object are copied. headers: { 'Cache-Control': 'no-cache', }, // Configure the meta parameter to specify the metadata of the destination object. If you do not configure the meta parameter, the metadata of the destination object is the same as the metadata of the source object. The metadata of the source object is copied. meta: { location: 'hangzhou', year: 2015, people: 'mary', }, }); console.log(result); } catch (e) { console.log(e); } } copySmallObjectBetweenBuckets()
Copy a large object
You can use the multipartUploadCopy
operation to copy an object whose size is greater than 1 GB in parts.
The following sample code provides an example on how to copy an object across buckets in the same region:
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 the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the name of the bucket.
bucket: "destexamplebucket",
});
async function copyLargeObjectBetweenDifferentBuckets() {
try {
const copyheaders = {
// If the ETag value of the source object is the same as the ETag value that is specified in the request, OSS copies the object. Otherwise, OSS returns the HTTP status code 412 (PreconditionFailed).
"x-oss-copy-source-if-match": "5B3C1A2E053D763E1B002CC607C5****",
// If the ETag value of the source object is different from the ETag value that is specified in the request, OSS copies the object and returns 200 OK. Otherwise, OSS returns the HTTP status code 304 (NotModified).
"x-oss-copy-source-if-none-match": "5B3C1A2E053D763E1B002CC607C5****",
// If the time that is specified in the request is later than or the same as the time when the object is modified, OSS copies the object and returns 200 OK. Otherwise, OSS returns the HTTP status code 412 (PreconditionFailed).
"x-oss-copy-source-if-unmodified-since": "2022-12-09T07:01:56.000Z",
// If the time that is specified in the request is earlier than the time when the object is modified, OSS copies the object and returns 200 OK. Otherwise, OSS returns the HTTP status code 304 (NotModified).
"x-oss-copy-source-if-modified-since": "2022-12-09T07:01:56.000Z",
};
const headers = {
// Specify the caching behavior of the web page when the object is downloaded.
"Cache-Control": "no-cache",
// Specify the name of the object when the object is downloaded.
"Content-Disposition": "somename",
// Specify the content encoding format when the object is downloaded.
"Content-Encoding": "utf-8",
// Specify the validity period. Unit: milliseconds.
Expires: "1000",
};
let savedCpt;
// Specify the full path of the destination object. Do not include the bucket name in the full path. Example: destexampleobject1.txt.
const r1 = await client.multipartUploadCopy("destexampleobject1.txt", {
// Specify the full path of the source object. Do not include the bucket name in the full path. Example: srcexampleobject.txt.
sourceKey: "srcexampleobject.txt",
// Specify the name of the source bucket. Example: sourcebucket.
sourceBucketName: "sourcebucket",
copyheaders: copyheaders,
});
console.log(r1);
// Specify the full path of the destination object. Do not include the bucket name in the full path. Example: destexampleobject 2.txt.
const r2 = await client.multipartUploadCopy("destexampleobject2.txt", {
// Specify the full path of the source object. Do not include the bucket name in the full path. Example: srcexampleobject.txt.
sourceKey: "srcexampleobject.txt",
// Specify the name of the source bucket. Example: sourcebucket.
sourceBucketName: "sourcebucket",
}, {
// Specify the maximum number of parts that can be uploaded in parallel.
parallel: 4,
// Specify the part size.
partSize: 1024 * 1024,
progress: function (p, cpt, res) {
console.log(p);
savedCpt = cpt;
console.log(cpt);
console.log(res.headers["x-oss-request-id"]);
},
headers: headers,
copyheaders: copyheaders,
});
console.log(r2);
// Specify the full path of the destination object. Do not include the bucket name in the full path. Example: destexampleobject3.txt.
const r3 = await client.multipartUploadCopy("destexampleobject3.txt", {
// Specify the full path of the source object. Do not include the bucket name in the full path. Example: srcexampleobject.txt.
sourceKey: "srcexampleobject.txt",
// Specify the name of the source bucket. Example: sourcebucket.
sourceBucketName: "sourcebucket",
}, {
checkpoint: savedCpt,
progress: function (p, cpt, res) {
console.log(p);
console.log(cpt);
console.log(res.headers["x-oss-request-id"]);
},
});
console.log(r3);
} catch (e) {
console.log(e);
}
}
copyLargeObjectBetweenDifferentBuckets()
References
Copy a small object
For the complete sample code that is used to copy a small object, visit GitHub.
For more information about the API operation that you can call to copy a small object, see CopyObject.
Copy a large object
For more information about the complete sample code that is used to copy a large object, visit GitHub.
For more information about the API operation that you can call to copy a large object, see UploadPartCopy.