file

Node.js installation

If you need to manage OSS storage space, upload and download files, manage data, and process images, you can first install the OSS Node.js SDK. This article describes how to install and use the OSS Node.js SDK.

prerequisite

  • AliCloud OSS service for object storage has been activated.

  • The AccessKey ID and AccessKey Secret of the RAM user have been created.

    Since the cloud account AccessKey has access to all APIs, it is recommended to use the RAM user's AccessKey. If it is deployed on the server, please use RAM or STS for API access or daily operation and maintenance control operations; If it is deployed on the client side, please use STS mode for API access. For details, see access control

background information

OSS Node.js SDK is based on Node.js Environment construction.

At present, the demo in the official website document is based on SDK 6. X. If the SDK version is lower than 6. X, see 5. X Development Documents , to upgrade to 6. X, see Upgrade Details

For SDK source code, see GitHub For more information, see API documentation

install

OSS supports Node.js version 8.0.0 and above. If you need to use it in an environment where Node.js is lower than 8.0.0, use the ali oss 4. x version.

use npm Install the SDK. The installation command is npm install ali-oss --save

Usage

Node.js SDK supports Promise Asynchronous programming. You can use async/await Syntax, or use then/catch Syntax to process Promise Object.

async/await

adopt async and await Keyword, you can write a structure similar to synchronization code, which is actually Promise Object to make the asynchronous process clearer. use async/await The code examples for uploading and downloading files are as follows:

 const OSS = require('ali-oss'); //Initialize the OSS client. Please replace the following parameters with your own configuration information. const client = new OSS({ Region: 'yourregion',//Example: 'oss cn hangzhou', fill in the bucket region. AccessKeyId: process.env.OSS_ACCESS_KEY_ID,//Ensure that the environment variable OSS_ACCESS_KEY_ID has been set. AccessKeySecret: process. env. OSS_ACCESS_KEY_SECRET,//Ensure that the environment variable OSS_ACCESS_KEY_SECRET has been set. Bucket: 'yourbucketname',//Example: 'my bucket name', fill in the name of the storage space. }); async function uploadAndDownloadFile() { try { //Upload a file to OSS. 'object' is the file name in OSS, and 'localfile' is the path of the local file. const uploadResult = await client.put('object', 'localfile'); Console. log ('Upload succeeded: ', uploadResult); //Download files from OSS to verify that the upload is successful. const getResult = await client.get('object'); Console. log ('Getting the file succeeded: ', getResult); } catch (error) { Console. error ('Error occurred: ', error); //Add error handling logic here. } } uploadAndDownloadFile();

then/catch

Similar to Callback, through Promise Object's then Method to handle the successful result of asynchronous operation, and use the catch Method to handle possible errors. use then/catch The code examples for uploading and downloading files are as follows:

 const OSS = require('ali-oss'); //Initialize the OSS client. Please replace the following parameters with your own configuration information. const client = new OSS({ Region: 'yourregion',//Example: 'oss cn hangzhou', fill in the bucket region. AccessKeyId: process.env.OSS_ACCESS_KEY_ID,//Ensure that the environment variable OSS_ACCESS_KEY_ID has been set. AccessKeySecret: process. env. OSS_ACCESS_KEY_SECRET,//Ensure that the environment variable OSS_ACCESS_KEY_SECRET has been set. Bucket: 'yourbucketname',//Example: 'my bucket name', fill in the name of the storage space. }); //Upload the file using the OSS put method, and then call then in a chained way to process the results or catch errors. client.put('object', 'localfile') .then((uploadResult) => { Console. log ('Upload succeeded: ', uploadResult); //Continue downloading the file to verify that the upload was successful. return client.get('object'); }) .then((getResult) => { Console. log ('Getting the file succeeded: ', getResult); }) .catch((error) => { Console. error ('Error occurred: ', error); //Add error handling logic here. });