Last update:2020-09-27 14:43:51
This document describes how to install our Cloud Storage SDK for Node.js.
npm install wcs-nodejs-sdk
Download source code from GitHub, and execute in root directory.
npm install
const wcs = require('wcs-nodejs-sdk');
let client = new wcs.wcsClient(config);
const wcs = require('wcs-nodejs-sdk/index');
let client = new wcs.wcsClient(config)
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;
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);
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);