const path = require('path');
const Controller = require('egg').Controller;
const fs = require('mz/fs');
// upload a file in controller
module.exports = class extends Controller {
async upload() {
const ctx = this.ctx;
const file = ctx.request.files[0];
const name = 'egg-oss-demo/' + path.basename(file.filename);
let result;
try {
result = await ctx.oss.put(name, file.filepath);
} finally {
await fs.unlink(file.filepath);
}
if (result) {
console.log('get oss object: %j', object);
ctx.unsafeRedirect(result.url);
} else {
ctx.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).
egg-oss
OSS plugin for egg
Install
Configration
To enable oss plugin, you should change
${baseDir}/config/plugin.jsThen fill in nessary information like OSS’s
bucket,accessKeyId,accessKeySecretin${baseDir}/config/config.{env}.jsMention,
egg-osssupport normal oss client and oss cluster client, based on oss-client:Init in egg agent, default is
false:Usage
You can aquire oss instance on
apporctx.The example below will upload file to oss using the
filemode of egg-multipart.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 byapp.oss.createInstance(config).${appdir}/config/config.default.js${appdir}/config/plugin.js${appdir}/app.jsDevelopment
Create
.envfile for environment that testcase need.And run
npm test.The key is saved in link (ask @popomore), you can change the key by run
scripts/gen_env.sh.Questions & Suggestions
Please open an issue here.
License
MIT