Skip to content

Commit 74d0c45

Browse files
chunge66lisong
authored andcommitted
Add storage logic for upyun cdn (#207)
1 parent 991599c commit 74d0c45

File tree

5 files changed

+125
-0
lines changed

5 files changed

+125
-0
lines changed

config/config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ config.development = {
2020
bucketName: "",
2121
downloadUrl: "" // Binary files download host address.
2222
},
23+
// Config for upyun (https://www.upyun.com/) storage when storageType value is "upyun"
24+
upyun: {
25+
storageDir: process.evv.UPYUN_STORAGE_DIR,
26+
serviceName: process.env.UPYUN_SERVICE_NAME,
27+
operatorName: process.env.UPYUN_OPERATOR_NAME,
28+
operatorPass: process.env.UPYUN_OPERATOR_PASS,
29+
downloadUrl: process.env.DOWNLOAD_URL,
30+
},
2331
// Config for Amazon s3 (https://aws.amazon.com/cn/s3/) storage when storageType value is "s3".
2432
s3: {
2533
accessKeyId: process.env.AWS_ACCESS_KEY_ID,

core/utils/common.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var config = require('../config');
77
var _ = require('lodash');
88
var validator = require('validator');
99
var qiniu = require("qiniu");
10+
var upyun = require('upyun');
1011
var common = {};
1112
var AppError = require('../app-error');
1213
var jschardet = require("jschardet");
@@ -225,6 +226,8 @@ common.uploadFileToStorage = function (key, filePath) {
225226
return common.uploadFileToOSS(key, filePath);
226227
} else if (storageType === 'qiniu') {
227228
return common.uploadFileToQiniu(key, filePath);
229+
} else if (storageType === 'upyun') {
230+
return common.uploadFileToUpyun(key, filePath);
228231
} else if (storageType === 'tencentcloud') {
229232
return common.uploadFileToTencentCloud(key, filePath);
230233
}
@@ -350,6 +353,43 @@ common.uploadFileToQiniu = function (key, filePath) {
350353
});
351354
};
352355

356+
common.uploadFileToUpyun = function (key, filePath) {
357+
var serviceName = _.get(config, "upyun.serviceName");
358+
var operatorName = _.get(config, "upyun.operatorName");
359+
var operatorPass = _.get(config, "upyun.operatorPass", "");
360+
var storageDir = _.get(config, "upyun.storageDir", "");
361+
var service = new upyun.Service(serviceName, operatorName, operatorPass);
362+
var client = new upyun.Client(service);
363+
return (
364+
new Promise((resolve, reject) => {
365+
client.makeDir(storageDir).then(result => {
366+
if(!storageDir) {
367+
reject(new AppError.AppError('Please config the upyun remoteDir!'));
368+
return;
369+
}
370+
let remotePath = storageDir + '/' + key;
371+
log.debug('uploadFileToUpyun remotePath:', remotePath);
372+
log.debug('uploadFileToUpyun mkDir result:', result);
373+
client.putFile(remotePath, fs.createReadStream(filePath)).then(data => {
374+
log.debug('uploadFileToUpyun putFile response:', data);
375+
if(data) {
376+
resolve(key)
377+
} else {
378+
log.debug('uploadFileToUpyun putFile failed!', data);
379+
reject(new AppError.AppError('Upload file to upyun failed!'));
380+
}
381+
}).catch(e1 => {
382+
log.debug('uploadFileToUpyun putFile exception e1:', e1);
383+
reject(new AppError.AppError(JSON.stringify(e1)));
384+
})
385+
}).catch(e => {
386+
log.debug('uploadFileToUpyun putFile exception e:', e);
387+
reject(new AppError.AppError(JSON.stringify(e)));
388+
});
389+
})
390+
);
391+
};
392+
353393
common.uploadFileToS3 = function (key, filePath) {
354394
var AWS = require('aws-sdk');
355395
return (

docs/process.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
"NODE_ENV" : "production",
1111
"PORT" : 3000,
1212
"CONFIG_FILE" : "/path/to/production/config.js"
13+
14+
// Must set add config when STORAGE_TYPE is upyun
15+
// "STORAGE_TYPE" : "upyun",
16+
// "DOWNLOAD_URL" : "",
17+
// "UPYUN_STORAGE_DIR" : "",
18+
// "UPYUN_SERVICE_NAME" : "",
19+
// "UPYUN_OPERATOR_NAME" : "",
20+
// "UPYUN_OPERATOR_PASS" : ""
1321
}
1422
}
1523
]

package-lock.json

Lines changed: 68 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"nodemailer": "^4.0.1",
6666
"pug": "^2.0.1",
6767
"qiniu": "^7.1.3",
68+
"upyun": "^3.3.9",
6869
"rand-token": "^0.4.0",
6970
"recursive-readdir": "^2.1.1",
7071
"redis": "^2.6.2",

0 commit comments

Comments
 (0)