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. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For details about supported regions and endpoints, see Regions and endpoints.
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 Configuration examples for common scenarios.
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.
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 Policy or Bucket policies.
API | Action | Definition |
CopyObject |
| Copies objects within a bucket or between buckets in the same region. |
| ||
| If you specify the source object version through versionId, this permission is also required. | |
| If you copy object tags through x-oss-tagging, these permissions are required. | |
| ||
| If you specify the tags of a specific version of the source object through versionId, this permission is also required. | |
| When copying an object, if the destination object metadata contains X-Oss-Server-Side-Encryption: KMS, these two permissions are required. | |
|
Copy a small object
You can use the copy method to copy an object that is no more than 1 GB in size within the same bucket or to a different bucket in the same region.
Copy an object within the same bucket
The following sample code shows 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 destination object and source object. Do not include the bucket name in the full paths. // Specify HTTP headers and custom 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 source object has not been modified since the specified time, the object is copied and 200 OK is returned. '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. Other users do not have permissions to access 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 regionThe following sample code shows how to copy an object across buckets in the same region.
The following sample code provides an example of 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 name of the destination object destobject.txt, the name of the source object srcobject.txt, and the name of the bucket to which the source object belongs. 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
For objects larger than 1 GB, you can use the multipartUploadCopy method to perform a multipart copy.
The following sample code shows 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 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 expiration time in 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: destexampleobject2.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",
}, {
// Set the number of parts that can be uploaded in parallel.
parallel: 4,
// Set 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 to copy a small object, see the GitHub example.
For more information about the API operation for copying a small object, see CopyObject.
Copy a large object
For the complete sample code to copy a large object, see the GitHub example.
For more information about the API operation for copying a large object, see UploadPartCopy.