Skip to content
Merged
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
6 changes: 6 additions & 0 deletions config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ config.development = {
bucketName: "",
downloadUrl: "" //文件下载域名地址
},
// conf for s3
s3: {
bucketName: process.env.BUCKET_NAME,
region: process.env.REGION,
downloadUrl: process.env.DOWNLOAD_URL,
},
//文件存储在本地配置 当storageType为local时需要配置
local: {
storageDir: "/Users/tablee/workspaces/storage",
Expand Down
31 changes: 31 additions & 0 deletions core/utils/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var unzip = require('node-unzip-2');
var config = require('../config');
var _ = require('lodash');
var qiniu = require("qiniu");
var AWS = require('aws-sdk');
var common = {};
module.exports = common;

Expand Down Expand Up @@ -112,6 +113,8 @@ common.uptoken = function (bucket, key) {
common.uploadFileToStorage = function (key, filePath) {
if (_.get(config, 'common.storageType') === 'local') {
return common.uploadFileToLocal(key, filePath);
} else if (_.get(config, 'common.storageType') === 's3') {
return common.uploadFileToS3(key, filePath);
}
return common.uploadFileToQiniu(key, filePath);
};
Expand Down Expand Up @@ -147,6 +150,8 @@ common.uploadFileToLocal = function (key, filePath) {
common.getDownloadUrl = function () {
if (_.get(config, 'common.storageType') === 'local') {
return _.get(config, 'local.downloadUrl');
} else if (_.get(config, 'common.storageType') === 's3') {
return _.get(config, 's3.downloadUrl');
}
return _.get(config, 'qiniu.downloadUrl');
}
Expand Down Expand Up @@ -185,6 +190,32 @@ common.uploadFileToQiniu = function (key, filePath) {
});
};

common.uploadFileToS3 = function (key, filePath) {
return (
new Promise(function(resolve, reject) {
AWS.config.update({
region: _.get(config, 's3.region')
});
var s3 = new AWS.S3({
params: {Bucket: _.get(config, 's3.bucketName')}
});
fs.readFile(filePath, function(err, data) {
s3.upload({
Key: key,
Body: data,
ACL:'public-read',
}, function(err, response) {
if(err) {
reject(new Error(JSON.stringify(err)));
} else {
resolve(response.ETag)
}
})
});
})
);
};

common.diffCollectionsSync = function (collection1, collection2) {
var diffFiles = [];
var collection1Only = [];
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"coverage": "make coverage"
},
"dependencies": {
"aws-sdk": "^2.7.0",
"bcrypt": "^0.8.7",
"bluebird": "^3.4.1",
"body-parser": "^1.15.2",
Expand Down