egg-oss
OSS plugin for egg
Install
$ npm i egg-oss
Configration
To enable oss plugin, you should change ${baseDir}/config/plugin.js
// config/plugin.jsexportsoss = enable: true package: 'egg-oss';
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:
// normal oss bucketexportsoss = client: accessKeyId: 'your access key' accessKeySecret: 'your access secret' bucket: 'your bucket name' endpoint: 'oss-cn-hongkong.aliyun.com' timeout: '60s' ; // cluster oss bucket// need to config all bucket information under clusterexportsoss = client: cluster: endpoint: 'host1' accessKeyId: 'id1' accessKeySecret: 'secret1' endpoint: 'host2' accessKeyId: 'id2' accessKeySecret: 'secret2' schedule: 'masterSlave' //default is `roundRobin` timeout: '60s' ;
Init in egg agent, default is false
:
exportsoss = useAgent: true;
Usage
You can aquire oss instance on app
or ctx
.
const path = ; // upload a file in controllermodule { const parts = this; let object; let part; part = parts; while part if partlength // arrays are busboy fields console; console; console; console; else // otherwise, it's a stream console; console; console; console; // file handle object = thisoss; part = parts; console; if object console; this; else thisbody = '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
exportsoss = clients: bucket1: bucket: 'bucket1' bucket2: bucket: 'bucket2' // shared by client, clients and createInstance default: endpoint: '' accessKeyId: '' accessKeySecret: '' ; exportsbucket3 = bucket: 'bucket3';
${appdir}/config/plugin.js
exportsoss = true;
${appdir}/app.js
module { const bucket1 = apposs; const bucket2 = apposs; // it will merge app.config.bucket3 and app.config.oss.default const bucket3 = apposs;}
Questions & Suggestions
Please open an issue here.
secure keys
ping @fengmk2 to give you the access key!