Quick start: NodeJS

Last Updated: Nov 07, 2017

This document explains how to use the OSS JavaScript SDK in a Node.js environment to access the OSS service, including viewing the bucket list, viewing the object list and uploading/downloading/deleting objects. To facilitate changes, how to create a new app.js is explained in the next document. The demo code of the following features is included in this object.

Install the SDK

First install ali-oss in the working directory:

  1. npm install ali-oss

Use the synchronous mode

SDK is developed on the basis of ES6. Using Generator Function enables users to use the code in synchronous mode. This feature must be used in combination with co. To use the code in synchronous mode, you must install Go:

  1. npm install co

Use the asynchronous mode

To support the callback usage, SDK provides asynchronous interfaces based on Promise. The usage is similar to the callback.

For example, the following section uses the synchronous mode:

Initialize the client

Create an object app.js and write the following content:

  1. var co = require('co');
  2. var OSS = require('ali-oss');
  3. var client = new OSS({
  4. region: '<Your region>',
  5. accessKeyId: '<Your AccessKeyId>',
  6. accessKeySecret: '<Your AccessKeySecret>'
  7. });

Specifically, the region parameter is the region used when you applied for the OSS service, such as oss-cn-hangzhou. For complete region list, see OSS Nodes.

If the used endpoint is not in the preceding list, you can specify the endpoint through the following parameters:

  • internal: Use in combination with region. If internal is specified to true, visit the intranet node.

  • secure: Use in combination with region. If secure is specified to true, use the HTTP for access.

  • endpoint: For example, http://oss-cn-hangzhou.aliyuncs.com. If endpoint is specified, region is ignored. You can specify HTTPS for endpoint, which can be in the IP address format.

  • cname: Use in combination with endpoint. If the cname is specified to true, the endpoint is regarded as the bounded user-defined domain of the user.

  • bucket: If the bucket is not specified, you must callthe useBucket interface first for object-related operations (only one call is required).

  • time-out: The default value is 60 seconds. This parameter specifies the API time-out value of OSS.

View the bucket list

Add the following content at the end of app.js. Use the listBuckets interface to view the bucket list:

  1. co(function* () {
  2. var result = yield client.listBuckets();
  3. console.log(result);
  4. }).catch(function (err) {
  5. console.log(err);
  6. });

Run node app.js and view the result.

View the object list

Modify app.js. Use the list interface to view the object list:

  1. co(function* () {
  2. client.useBucket('Your bucket name');
  3. var result = yield client.list({
  4. 'max-keys': 5
  5. });
  6. console.log(result);
  7. }).catch(function (err) {
  8. console.log(err);
  9. });

Run node app.js and view the result.

Upload an object

Modify app.js. Use the put interface to upload the object:

  1. co(function* () {
  2. client.useBucket('Your bucket name');
  3. var result = yield client.put('object-key', 'local file');
  4. console.log(result);
  5. }).catch(function (err) {
  6. console.log(err);
  7. });

Download an object

Modify app.js. Use the get interface to download an object:

  1. co(function* () {
  2. var result = yield client.get('object-key', 'local file');
  3. console.log(result);
  4. }).catch(function (err) {
  5. console.log(err);
  6. });

Delete an object

Modify app.js. Use the delete interface to delete the object:

  1. co(function* () {
  2. var result = yield client.delete('object-key');
  3. console.log(result);
  4. }).catch(function (err) {
  5. console.log(err);
  6. });

Learn more

Thank you! We've received your feedback.