All Products
Search
Document Center

Object Storage Service:Installation

Last Updated:Sep 11, 2023

This topic describes how to install and use Object Storage Service (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 code in the official document is based on OSS SDK for Node.js 6.x. For more information about OSS SDK for Node.js earlier than 6.x, visit OSS SDK for Node.js 5.x. For more information about how to upgrade OSS SDK for Node.js to 6.x, visit 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, see API reference.

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.

How to use OSS SDK for Node.js

You can use OSS SDK for Node.js in synchronous and asynchronous modes. new OSS() is used to create OSS clients regardless of the mode.

  • Synchronous mode: The async and await methods are used to synchronize asynchronous operations.

  • Asynchronous mode: Perform asynchronous operations in the same manner as you perform a callback. The API operation returns a Promise object for a request. The .then() method is used to process the returned result, and the .catch() method is used to fix errors.

The following sample code provides examples on how to upload an object in synchronous mode and download an object in asynchronous mode:

  • Synchronous mode

    let client = new OSS(...);
    async function put () {
      try {
        // Set 'object' to the name of the object uploaded to OSS. The name is a full path that cannot contain bucket names. 
        // Set 'localfile' to the full path of the local file from which the object is uploaded to OSS. 
        let r1 = await client.put('object','localfile'); 
        console.log('put success: %j', r1);
        let r2 = await client.get('object');
        console.log('get success: %j', r2);
      } catch(e) {
        console.error('error: %j', e);
      }
    }
    put();
  • Asynchronous mode

    let client = new OSS(...);
    
    // Set 'object' to the name of the object downloaded from OSS. The name is a full path that cannot contain bucket names. 
    // Set 'localfile' to the full path of the local file to which the object is downloaded. 
    client.put('object', 'localfile').then(function (r1) {
      console.log('put success: %j', r1);
      return client.get('object');
    }).then(function (r2) {
      console.log('get success: %j', r2);
    }).catch(function (e) {
      console.error('error: %j', e);
    });