This topic describes how to download an object from a bucket to your computer.
Sample code
The following sample code provides an example on how to download an object named exampleobject.txt in a bucket named examplebucket to the D:\localpath path in a local disk:
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 Region to oss-cn-hangzhou.
region: 'yourRegion',
// Obtain access credentials from environment variables. Before running this 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,
authorizationV4: true,
// Specify the bucket name.
bucket: 'examplebucket'
});
async function get () {
try {
// Specify the full path of the object and the full path of the local file. The full path of the object cannot contain the bucket name.
// If the specified local file exists, it is overwritten. If the file does not exist, it is created.
// If a local path is not specified, the downloaded file is saved to the local path of the project by default.
const result = await client.get('exampleobject.txt', 'D:\\localpath\\examplefile.txt');
console.log(result);
} catch (e) {
console.log(e);
}
}
get();