All Products
Search
Document Center

Object Storage Service:Installation

Last Updated:Jan 12, 2024

If you want to use Object Storage Service (OSS) SDK for Node.js to manage buckets, upload and download objects, manage data, and perform image processing (IMG), install the SDK first. This topic describes how to install and use OSS SDK for Node.js.

Prerequisites

  • OSS is activated.

  • The AccessKey pair of a RAM user is created. The AccessKey pair consists of the AccessKey ID and the AccessKey secret.

    The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. If you deploy your service on the server, you can use a RAM user or Security Token Service (STS) credentials to call API operations or perform routine operations. If you deploy your service on the client, use STS credentials to call API operations. For more information, visit Resource Access Management.

Background information

OSS SDK for Node.js is built based on the Node.js environment.

The sample project in the official document is based on OSS SDK for Node.js 6.X. If you are using an earlier version of the SDK, see OSS SDK for Node.js documentation. To upgrade your SDK to version 6.X, see Upgrading Notes (5.x to 6.x).

For more information about the source code of OSS SDK for Node.js, visit GitHub. For more information about code examples, see API documentation.

Install OSS SDK for Node.js

OSS supports Node.js 8.0.0 and later. To use OSS SDK for Node.js in Node.js earlier than 8.0.0, use OSS SDK for Node.js V4.x.

Run npm install ali-oss --save to install the SDK package. For more information, visit npm.

If network issues occur when you use npm, we recommend that you use cnpm, which is the npm image provided by Taobao.

Use OSS SDK for Node.js

OSS SDK for Node.js supports Promise-based asynchronous programming. You can use async/await or then/catch to process Promise objects.

async/await

You can use the async and await keywords to write code that is similar to synchronous code. The keywords actually processes Promise objects and makes asynchronous code clearer. The following sample code provides an example on how to upload and download objects by using the async/await syntax.

const OSS = require('ali-oss');

// Initialize the OSS client. Replace the following parameters with your actual configuration information. 
const client = new OSS({
  region: 'yourregion', // Specify the region in which the bucket resides. Example: 'oss-cn-hangzhou'. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID, // Make sure that the OSS_ACCESS_KEY_ID environment variable is configured. 
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET, // Make sure that the OSS_ACCESS_KEY_SECRET environment variable is configured. 
  bucket: 'yourbucketname', // Specify the name of the bucket. Example: 'my-bucket-name'. 
});

async function uploadAndDownloadFile() {
  try {
    // Upload an object to OSS. Replace 'object' with the name that you want the object to have in OSS. Replace 'localfile' with the path of the local file that you want to upload. 
    const uploadResult = await client.put('object', 'localfile');
    console.log('Upload succeeded:', uploadResult);
    // Download the object from OSS to verify that the upload was successful. 
    const getResult = await client.get('object');
    console.log('Object downloaded:', getResult);
  } catch (error) {
    console.error('Error:', error);
    // Write your error handling code here. 
  }
}

uploadAndDownloadFile();

then/catch

You can use the then method of the Promise object to process a successful synchronous operation and the catch method to handle errors. The following sample code provides an example on how to upload and download an object by using then/catch:

const OSS = require('ali-oss');

// Initialize the OSS client. Replace the following parameters with your actual configuration information. 
const client = new OSS({
  region: 'yourregion', // Specify the region in which the bucket resides. Example: 'oss-cn-hangzhou'. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID, // Make sure that the OSS_ACCESS_KEY_ID environment variable is configured. 
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET, // Make sure that the OSS_ACCESS_KEY_SECRET environment variable is configured. 
  bucket: 'yourbucketname', // Specify the name of the bucket. Example: 'my-bucket-name'. 
});

// Use the PUT method to upload an object to OSS and call then() in a chained way to process the result or handle an error. 
client.put('object', 'localfile')
  .then((uploadResult) => {
    console.log('Upload succeeded:', uploadResult);
    // Download the object to verify that the upload was successful. 
    return client.get('object');
  })
  .then((getResult) => {
    console.log('Object downloaded:', getResult);
  })
  .catch((error) => {
    console.error('Error:', error);
    // Write your error handling code here. 
  });