Node.js

Last update:2020-09-27 14:43:51

This document describes how to install our Cloud Storage SDK for Node.js.

Prerequisites

  • Cloud Storage is activated.
  • The AccessKey and SecretKey are created

Install SDK for Node.js

Install it by npm

npm install wcs-nodejs-sdk

Install it manually

Download source code from GitHub, and execute in root directory.

npm install

How to use

  • If you install it by npm, please use the below require command.
const wcs = require('wcs-nodejs-sdk');

let client = new wcs.wcsClient(config);
  • If you install it manually, please require the index.js under the root directory.
const wcs = require('wcs-nodejs-sdk/index');

let client = new wcs.wcsClient(config)

Configuration

Create a config file, the detailed configurations are as below.

var config = {
    AccessKey: '<YOUR ACCESS KEY>',         // The accesskey.
    SecretKey: '<YOUR SECRET KEY>',         // The secretkey
    UploadDomain: '<YOUR UPLOAD DOMAIN>',   // The upload domain (don't need the prefix of http)
    MgrDomain: '<YOUR MGR DOOMAIN>',        // The management domain (don't need the prefix of http) 
    BlockSize: <size of block>,             // Default value is 4194304(4M, we don't recommned you to change it), this value must be greater than 4M while the multiples of 4M, and it must be an int.
    HttpTimeout: <http timeout>,          // http timeout, the unit is ms. Default value is 120000(120s), and it must be an int.
};

module.exports = config;

Upload - normal upload

Normal upload will upload the file to the cloud storage in one time operation. It is recommended to use normal upload only when the file size is less than 20M.

Example

const wcs = require('wcs-nodejs-sdk');
const config = require('./config');

let putPolicy = {
    scope: bucket+':'+key,
    deadline: '<deadline>',
};

let client = new wcs.wcsClient(config);
var callback = function(err, data, res) {
    console.log('callback');
    if (err) {
        console.log(err);
    }
    else {
        console.log(res.statusCode);
        console.log(wcs.utils.urlSafeBase64Decode(data));
    }
}

// normal upload
let filePath = __dirname+'/test';
client.uploadByPath(filePath, putPolicy, null, callback);

Upload-Multipart upload

  • When the file is large, normal uploads are prone to upload abnormally such as timeouts. It is recommended to use multipart upload when the file size is over 20M.
  • You can customize the chunk size when using multipart upload, but the configured chunk size must be greater than 4M and an integer multiple of 4M.
  • When using multipart upload, if you set the upload record saving in the specified file, the breakpoint continuation function will be enabled if the same file is uploaded again after the upload is interrupted.

Example

const wcs = require('wcs-nodejs-sdk');
const config = require('./config');

let putPolicy = {
    scope: bucket+':'+key,
    deadline: '<deadline>',
};

let client = new wcs.wcsClient(config);
var callback = function(err, data, res) {
    console.log('callback');
    if (err) {
        console.log(err);
    }
    else {
        console.log(res.statusCode);
        console.log(wcs.utils.urlSafeBase64Decode(data));
    }
}

// Callback the status of multipart upload
var progressCallback = function(readLength, fileSize) {
    console.log(readLength + '/' + fileSize + ' finished');
}

// Multipart upload
let filePath = __dirname+'/test20M';

// If not specify the recordFile, breakpoint upload won't be enabled
let recordFile = __dirname+'/resume.record';
let extraParams = {
    deadline:3,                         // Deadline, unit is Day
    mimeType: 'application/text',       // Customized file mimeType
    progressCallback: progressCallback, // Callback multipart upload status
    params: {'x:myVar': 'myvar'},       // Custom variables
}
client.resumeUploadByPath(filePath, putPolicy, extraParams, callback);
Is the content of this document helpful to you?
Yes
I have suggestion
Submitted successfully! Thank you very much for your feedback, we will continue to strive to do better!