const Koa = require('koa');
const Router = require('koa-router');
const { koaBody } = require('koa-body');
const Minio = require('minio')
const path = require('path');
const app = new Koa();
app.use(koaBody({
multipart:true
}));
const router = new Router();
// 定义路由
router.get('/', async (ctx, next) => {
ctx.body = 'Hello, World!';
await next();
});
router.post('/upload', async (ctx, next) => {
var minioClient = new Minio.Client({
endPoint: '127.0.0.1',
port: 9000,
useSSL: false,
accessKey: 'zZxguyuuVCblUiM4yyAy',
secretKey: '3Gp6YbvQM7AQjuPJYRwVpIyEpGxq8Y3qEOgsXbk0',
})
const files = ctx.request.files
for(let i in files){
let metaData={
'Content-Type': files[i].mimetype
}
let name= files[i].newFilename+path.extname(files[i].originalFilename)
let res= await minioClient.fPutObject('ceshi',name, files[i].filepath, metaData)
console.log(res)
ctx.body="http://127.0.0.1:9000/ceshi/"+name
}
await next();
});
// 启动服务器
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(3000, () => {
console.log('Server running on http://localhost:3000');
});
