Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
29 changes: 29 additions & 0 deletions core/utils/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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 = [];
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down