$ npm i @shuang6/egg-cos --save
// {app_root}/config/plugin.js
exports.cos = {
enable: true,
package: '@shuang6/egg-cos'
};
// {app_root}/config/config.default.js
exports.cos = {
client: {
SecretId: '',
SecretKey: '',
Bucket: '',
Region: ''
}
};
Init in egg agent, default is false
:
exports.cos = {
useAgent: true
};
You can aquire tencent cloud cos instance on app
or ctx
.
const path = require('path');
const Controller = require('egg').Controller;
// upload a file in controller
module.exports = class extends Controller {
async upload() {
const ctx = this.ctx;
// please enable `file` mode of `egg-multipart`.
const file = ctx.request.files[0];
const name = 'egg-cos/' + path.basename(file.filename);
let result;
try {
result = await ctx.cos.put(name, file.filepath);
} finally {
// need to remove the tmp files
await ctx.cleanupRequestFiles();
}
if (result) {
ctx.logger.info('cos response:\n', result);
ctx.body = {
url: `https://${result.Location}`
};
} else {
ctx.body = 'please select a file to upload!';
}
}
};
Please open an issue here.