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
8 changes: 8 additions & 0 deletions config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ config.development = {
bucketName: "",
downloadUrl: "" // Binary files download host address.
},
// Config for upyun (https://www.upyun.com/) storage when storageType value is "upyun"
upyun: {
storageDir: process.evv.UPYUN_STORAGE_DIR,
serviceName: process.env.UPYUN_SERVICE_NAME,
operatorName: process.env.UPYUN_OPERATOR_NAME,
operatorPass: process.env.UPYUN_OPERATOR_PASS,
downloadUrl: process.env.DOWNLOAD_URL,
},
// Config for Amazon s3 (https://aws.amazon.com/cn/s3/) storage when storageType value is "s3".
s3: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
Expand Down
40 changes: 40 additions & 0 deletions core/utils/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var config = require('../config');
var _ = require('lodash');
var validator = require('validator');
var qiniu = require("qiniu");
var upyun = require('upyun');
var common = {};
var AppError = require('../app-error');
var jschardet = require("jschardet");
Expand Down Expand Up @@ -225,6 +226,8 @@ common.uploadFileToStorage = function (key, filePath) {
return common.uploadFileToOSS(key, filePath);
} else if (storageType === 'qiniu') {
return common.uploadFileToQiniu(key, filePath);
} else if (storageType === 'upyun') {
return common.uploadFileToUpyun(key, filePath);
} else if (storageType === 'tencentcloud') {
return common.uploadFileToTencentCloud(key, filePath);
}
Expand Down Expand Up @@ -350,6 +353,43 @@ common.uploadFileToQiniu = function (key, filePath) {
});
};

common.uploadFileToUpyun = function (key, filePath) {
var serviceName = _.get(config, "upyun.serviceName");
var operatorName = _.get(config, "upyun.operatorName");
var operatorPass = _.get(config, "upyun.operatorPass", "");
var storageDir = _.get(config, "upyun.storageDir", "");
var service = new upyun.Service(serviceName, operatorName, operatorPass);
var client = new upyun.Client(service);
return (
new Promise((resolve, reject) => {
client.makeDir(storageDir).then(result => {
if(!storageDir) {
reject(new AppError.AppError('Please config the upyun remoteDir!'));
return;
}
let remotePath = storageDir + '/' + key;
log.debug('uploadFileToUpyun remotePath:', remotePath);
log.debug('uploadFileToUpyun mkDir result:', result);
client.putFile(remotePath, fs.createReadStream(filePath)).then(data => {
log.debug('uploadFileToUpyun putFile response:', data);
if(data) {
resolve(key)
} else {
log.debug('uploadFileToUpyun putFile failed!', data);
reject(new AppError.AppError('Upload file to upyun failed!'));
}
}).catch(e1 => {
log.debug('uploadFileToUpyun putFile exception e1:', e1);
reject(new AppError.AppError(JSON.stringify(e1)));
})
}).catch(e => {
log.debug('uploadFileToUpyun putFile exception e:', e);
reject(new AppError.AppError(JSON.stringify(e)));
});
})
);
};

common.uploadFileToS3 = function (key, filePath) {
var AWS = require('aws-sdk');
return (
Expand Down
8 changes: 8 additions & 0 deletions docs/process.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
"NODE_ENV" : "production",
"PORT" : 3000,
"CONFIG_FILE" : "/path/to/production/config.js"

// Must set add config when STORAGE_TYPE is upyun
// "STORAGE_TYPE" : "upyun",
// "DOWNLOAD_URL" : "",
// "UPYUN_STORAGE_DIR" : "",
// "UPYUN_SERVICE_NAME" : "",
// "UPYUN_OPERATOR_NAME" : "",
// "UPYUN_OPERATOR_PASS" : ""
}
}
]
Expand Down
68 changes: 68 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"nodemailer": "^4.0.1",
"pug": "^2.0.1",
"qiniu": "^7.1.3",
"upyun": "^3.3.9",
"rand-token": "^0.4.0",
"recursive-readdir": "^2.1.1",
"redis": "^2.6.2",
Expand Down