egg-oss-d
v1.0.0
Published
``` $ npm i egg-oss ``` To enable oss plugin, you should change ${baseDir}/config/plugin.js
Downloads
4
Readme
egg-oss
Install
$ npm i egg-oss
To enable oss plugin, you should change ${baseDir}/config/plugin.js
// config/plugin.js
exports.oss = {
enable: true,
package: 'egg-oss',
};
Then fill in nessary information like OSS's bucket, accessKeyId, accessKeySecret in ${baseDir}/config/config.{env}.js
//config/config.default.js
exports.oss = {
client: {
accessKeyId: 'your access key',
accessKeySecret: 'your access secret',
bucket: 'your bucket name',
endpoint: 'oss-cn-hongkong.aliyun.com',
timeout: '60s',
},
};
//file
exports.multipart = {
mode: 'file',
};
Usage
You can aquire oss instance on app or ctx.
The example below will upload file to oss using the file mode of egg-multipart.
const path = require('path');
const Controller = require('egg').Controller;
const fs = require('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/' + new Date().getTime()+ path.basename(file.filename);
let result;
try {
result = await ctx.oss.put(name, file.filepath);
} finally {
await fs.unlinkSync(file.filepath);
}
if (result) {
ctx.body={
code:1,
url:result.url,
msg:'成功'
}
} else {
ctx.body={
code:0,
msg:'失败'
}
}
}
};
vue
npx cnpm i element-ui -D
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);
action="/api/upload"
handleAvatarSuccess(res, file) {
this.imageUrl = res.url
},