diff --git a/config/config.js b/config/config.js index 8d8d4bba..0490837c 100644 --- a/config/config.js +++ b/config/config.js @@ -22,6 +22,15 @@ config.development = { region: process.env.REGION, downloadUrl: process.env.DOWNLOAD_URL, // binary files download host address. }, + // Config for Aliyun OSS (https://www.aliyun.com/product/oss) when storageType value is "oss". + oss: { + accessKeyId: "", + secretAccessKey: "", + endpoint: "", + bucketName: "", + prefix: "", // Key prefix in object key + downloadUrl: "", // binary files download host address. + }, // Config for local storage when storageType value is "local". local: { // Binary files storage dir, Do not use tmpdir and it's public download dir. diff --git a/core/utils/common.js b/core/utils/common.js index a399d1de..bca8de26 100644 --- a/core/utils/common.js +++ b/core/utils/common.js @@ -114,6 +114,8 @@ common.uploadFileToStorage = function (key, filePath) { return common.uploadFileToLocal(key, filePath); } else if (_.get(config, 'common.storageType') === 's3') { return common.uploadFileToS3(key, filePath); + } else if (_.get(config, 'common.storageType') === 'oss') { + return common.uploadFileToOSS(key, filePath); } return common.uploadFileToQiniu(key, filePath); }; @@ -151,6 +153,8 @@ common.getDownloadUrl = function () { return _.get(config, 'local.downloadUrl'); } else if (_.get(config, 'common.storageType') === 's3') { return _.get(config, 's3.downloadUrl'); + } else if (_.get(config, 'common.storageType') === 'oss') { + return _.get(config, 'oss.downloadUrl'); } return _.get(config, 'qiniu.downloadUrl'); } @@ -216,6 +220,31 @@ common.uploadFileToS3 = function (key, filePath) { ); }; +common.uploadFileToOSS = function (key, filePath) { + var ALY = require('aliyun-sdk'); + var ossStream = require('aliyun-oss-upload-stream')(new ALY.OSS({ + accessKeyId: _.get(config, 'oss.accessKeyId'), + secretAccessKey: _.get(config, 'oss.secretAccessKey'), + endpoint: _.get(config, 'oss.endpoint'), + apiVersion: '2013-10-15', + })); + var upload = ossStream.upload({ + Bucket: _.get(config, 'oss.bucketName'), + Key: `${_.get(config, 'oss.prefix')}/${key}`, + }); + + return new Promise(function (resolve, reject) { + upload.on('error', function (error) { + reject(error); + }); + + upload.on('uploaded', function (details) { + resolve(details.ETag); + }); + fs.createReadStream(filePath).pipe(upload); + }); +}; + common.diffCollectionsSync = function (collection1, collection2) { var diffFiles = []; var collection1Only = []; diff --git a/package.json b/package.json index 259b606e..fa478a3f 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,8 @@ "coverage": "make coverage" }, "dependencies": { + "aliyun-oss-upload-stream": "^1.3.0", + "aliyun-sdk": "^1.9.17", "aws-sdk": "^2.7.0", "bcryptjs": "^2.3.0", "bluebird": "^3.4.1",