From 2ef7a84c242f2ef65bbf7abc8d8ef93e8dc51fb0 Mon Sep 17 00:00:00 2001 From: starlight36 Date: Tue, 10 Jan 2017 16:44:33 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Add=20Aliyun=20OSS=20storage=20support?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.js | 9 +++++++++ core/utils/common.js | 29 +++++++++++++++++++++++++++++ package.json | 2 ++ 3 files changed, 40 insertions(+) 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", From a00ccf68e8b9884ab4020b74ef01088ce5497c85 Mon Sep 17 00:00:00 2001 From: starlight36 Date: Tue, 10 Jan 2017 16:52:12 +0800 Subject: [PATCH 2/2] Add Aliyun OSS storage support, add Readme.md --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 46fc7cfc..63b20fef 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,15 @@ $ vim config/config.js bucketName: "", downloadUrl: "" //文件下载域名地址 }, + //阿里云存储配置 当storageType为oss时需要配置 + oss: { + accessKeyId: "", + secretAccessKey: "", + endpoint: "", + bucketName: "", + prefix: "", // 对象Key的前缀,允许放到子文件夹里面 + downloadUrl: "", // 文件下载域名地址,需要包含前缀 + }, //文件存储在本地配置 当storageType为local时需要配置 local: { storageDir: "/Users/tablee/workspaces/storage",