Restore a file (Node.js SDK)
Updated at:
In a bucket with versioning enabled, different versions of an object can have different storage classes. By default, the RestoreObject API operation restores the current version of an object. To restore a specific version, you must specify its versionId.
Sample code
The following code provides an example of how to restore a file:
const OSS = require('ali-oss');
const client = new OSS({
// Replace yourregion with the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set Region to oss-cn-hangzhou.
region: 'yourregion',
// Obtain access credentials from environment variables. Before running the sample code, make sure that 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,
// Replace yourbucketname with the bucket name.
bucket: 'yourbucketname',
});
const name = 'yourobjectName';
// Restore the object of a specific version ID.
const versionId = 'your versionId';
async function restore() {
const result = await client.restore(name, {
versionId,
});
console.log(result);
}
restore();
References
For more information about restoring files, see the RestoreObject API operation.
Is this page helpful?