koajs使用minio上传文件

由于未提供博客具体内容,无法给出包含关键信息的摘要。
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');
});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值