Skip to content

Commit 37cf83c

Browse files
starlight36lisong
authored andcommitted
Add Aliyun OSS storage support (lisong#35)
* Add Aliyun OSS storage support。 * Add Aliyun OSS storage support, add Readme.md
1 parent c36aa79 commit 37cf83c

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ $ vim config/config.js
7878
bucketName: "",
7979
downloadUrl: "" //文件下载域名地址
8080
},
81+
//阿里云存储配置 当storageType为oss时需要配置
82+
oss: {
83+
accessKeyId: "",
84+
secretAccessKey: "",
85+
endpoint: "",
86+
bucketName: "",
87+
prefix: "", // 对象Key的前缀,允许放到子文件夹里面
88+
downloadUrl: "", // 文件下载域名地址,需要包含前缀
89+
},
8190
//文件存储在本地配置 当storageType为local时需要配置
8291
local: {
8392
storageDir: "/Users/tablee/workspaces/storage",

config/config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ config.development = {
2222
region: process.env.REGION,
2323
downloadUrl: process.env.DOWNLOAD_URL, // binary files download host address.
2424
},
25+
// Config for Aliyun OSS (https://www.aliyun.com/product/oss) when storageType value is "oss".
26+
oss: {
27+
accessKeyId: "",
28+
secretAccessKey: "",
29+
endpoint: "",
30+
bucketName: "",
31+
prefix: "", // Key prefix in object key
32+
downloadUrl: "", // binary files download host address.
33+
},
2534
// Config for local storage when storageType value is "local".
2635
local: {
2736
// Binary files storage dir, Do not use tmpdir and it's public download dir.

core/utils/common.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ common.uploadFileToStorage = function (key, filePath) {
114114
return common.uploadFileToLocal(key, filePath);
115115
} else if (_.get(config, 'common.storageType') === 's3') {
116116
return common.uploadFileToS3(key, filePath);
117+
} else if (_.get(config, 'common.storageType') === 'oss') {
118+
return common.uploadFileToOSS(key, filePath);
117119
}
118120
return common.uploadFileToQiniu(key, filePath);
119121
};
@@ -151,6 +153,8 @@ common.getDownloadUrl = function () {
151153
return _.get(config, 'local.downloadUrl');
152154
} else if (_.get(config, 'common.storageType') === 's3') {
153155
return _.get(config, 's3.downloadUrl');
156+
} else if (_.get(config, 'common.storageType') === 'oss') {
157+
return _.get(config, 'oss.downloadUrl');
154158
}
155159
return _.get(config, 'qiniu.downloadUrl');
156160
}
@@ -216,6 +220,31 @@ common.uploadFileToS3 = function (key, filePath) {
216220
);
217221
};
218222

223+
common.uploadFileToOSS = function (key, filePath) {
224+
var ALY = require('aliyun-sdk');
225+
var ossStream = require('aliyun-oss-upload-stream')(new ALY.OSS({
226+
accessKeyId: _.get(config, 'oss.accessKeyId'),
227+
secretAccessKey: _.get(config, 'oss.secretAccessKey'),
228+
endpoint: _.get(config, 'oss.endpoint'),
229+
apiVersion: '2013-10-15',
230+
}));
231+
var upload = ossStream.upload({
232+
Bucket: _.get(config, 'oss.bucketName'),
233+
Key: `${_.get(config, 'oss.prefix')}/${key}`,
234+
});
235+
236+
return new Promise(function (resolve, reject) {
237+
upload.on('error', function (error) {
238+
reject(error);
239+
});
240+
241+
upload.on('uploaded', function (details) {
242+
resolve(details.ETag);
243+
});
244+
fs.createReadStream(filePath).pipe(upload);
245+
});
246+
};
247+
219248
common.diffCollectionsSync = function (collection1, collection2) {
220249
var diffFiles = [];
221250
var collection1Only = [];

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
"coverage": "make coverage"
3636
},
3737
"dependencies": {
38+
"aliyun-oss-upload-stream": "^1.3.0",
39+
"aliyun-sdk": "^1.9.17",
3840
"aws-sdk": "^2.7.0",
3941
"bcryptjs": "^2.3.0",
4042
"bluebird": "^3.4.1",

0 commit comments

Comments
 (0)