Quick start: NodeJS

Last Updated: Jun 06, 2017

Next we will explain 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, next we will introduce how to create a new ‘app.js’. The demo code of the features below is included in this object.

Install the SDK

First install ‘ali-oss’ in the working directory:

  1. npm install ali-oss

Use synchronous mode

The SDK is developed based on ES6. Using Generator Function enables users to use the code in synchronous mode. This feature should be used in combination with ‘co’. For details, refer to the Blog. To use the code in synchronous mode, you should also install ‘Go’:

  1. npm install co

Use the asynchronous mode

To support the callback usage, the SDK also provides asynchronous interfaces based on Promise. The usage is similar to the callback. For details, refer to the Blog.

The document below takes synchronous mode as an example.

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’. The completeregion list can be viewed in OSS Nodes.

If the used endpoint is not in the above 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’ will be 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’ will be regarded as thebounded user-defined domain of the user.
  • bucket: If the ‘bucket’ is not specified, you need to callthe ‘useBucket’ interface first for object-related operations (only one call is required)
  • timeout: The default value is 60 seconds. This parameter specifies the API timeout value of the 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 an 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 download an 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.