egg-oss

Install
$ npm i egg-oss
Configration
To enable oss plugin, you should change ${baseDir}/config/plugin.js
exports.oss = true;
Then fill in nessary information like OSS's bucket, accessKeyId, accessKeySecret in ${baseDir}/config/config.{env}.js
Mention, egg-oss support normal oss client and oss cluster client, based on ali-oss:
exports.oss = {
client: {
accessKeyId: 'your access key',
accessKeySecret: 'your access secret',
bucket: 'your bucket name',
endpoint: 'oss-cn-hongkong.aliyun.com',
timeout: '60s',
},
};
exports.oss = {
client: {
cluster: [{
endpoint: 'host1',
accessKeyId: 'id1',
accessKeySecret: 'secret1',
}, {
endpoint: 'host2',
accessKeyId: 'id2',
accessKeySecret: 'secret2',
}],
schedule: 'masterSlave',
timeout: '60s',
},
};
Init in egg agent, default is false:
exports.oss = {
useAgent: true,
};
Usage
You can aquire oss instance on app or ctx.
const path = require('path');
module.exports = function*() {
const parts = this.multipart();
let object;
let part;
while ((part = yield parts) !== null) {
if (part.length) {
console.log('field: ' + part[0]);
console.log('value: ' + part[1]);
console.log('valueTruncated: ' + part[2]);
console.log('fieldnameTruncated: ' + part[3]);
} else {
console.log('field: ' + part.fieldname);
console.log('filename: ' + part.filename);
console.log('encoding: ' + part.encoding);
console.log('mime: ' + part.mime);
object = yield this.oss.put('egg-oss-demo-' + part.filename, part);
}
}
console.log('and we are done parsing the form!');
if (object) {
console.log('get oss object: %j', object);
this.unsafeRedirect(object.url);
} else {
this.body = 'please select a file to upload!';
}
}
To learn OSS client API, please check oss document。
Create one more OSS buckets
Some application need to access more than one oss bucket, then you need to configure oss.clients, and
you can create new oss instance dynamicly by app.oss.createInstance(config).
${appdir}/config/config.default.js
exports.oss = {
clients: {
bucket1: {
bucket: 'bucket1',
},
bucket2: {
bucket: 'bucket2',
},
},
default: {
endpoint: '',
accessKeyId: '',
accessKeySecret: '',
},
};
exports.bucket3 = {
bucket: 'bucket3',
};
${appdir}/config/plugin.js
exports.oss = true;
module.exports = function (app) {
const bucket1 = app.oss.get('bucket1');
const bucket2 = app.oss.get('bucket2');
const bucket3 = app.oss.createInstance(app.config.bucket3);
}
Questions & Suggestions
Please open an issue here.
License
MIT