如何使用multer获取s3存储桶上传的对象url? [英] How to get s3 bucket uploaded object url using multer?

查看:25
本文介绍了如何使用multer获取s3存储桶上传的对象url?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码已成功在 s3 上上传对象,但我无法弄清楚如何获取上传的对象 url 返回.

This code is upload objects on s3 successfully but I was not able to figure out how to get uploaded object url return.

代码片段

let s3 = new aws.S3({
        accessKeyId: 'XXXXXXXXXXXXX',
        secretAccessKey: 'XXXXXXXXXXXXXXXXXXXXXXXXX',
        region: 'XXXXXXXX'
    });
    let upload = multer({
        storage: multerS3({
            s3: s3,
            acl: 'public-read',
            bucket: 'XXXXXXXXXXx',
            metadata: (req: any, file: any, cb: any) => {                    
                cb(null, { fieldName: file.fieldname });
            },
            key: (req: any, file: any, cb: any) => {
                cb(null, Date.now().toString() + '-' + file.originalname)
            }
        })
    });

    routes.post(`api/upload-image`, upload.single('photo'), async (req: any, res: Response) => {
        
        res.send('Image uploaded successfully');
    });

有没有办法或者回调获取url?

Is there any way or callback for getting the url?

推荐答案

let s3 = new aws.S3({
        accessKeyId: 'XXXXXXXXXXXXX',
        secretAccessKey: 'XXXXXXXXXXXXXXXXXXXXXXXXX',
        region: 'XXXXXXXX'
    });
    let upload = multer({
        storage: multerS3({
            s3: s3,
            acl: 'public-read',
            bucket: 'XXXXXXXXXXx',
            metadata: (req: any, file: any, cb: any) => {                    
                cb(null, { fieldName: file.fieldname });
            },
            key: (req: any, file: any, cb: any) => {
                cb(null, Date.now().toString() + '-' + file.originalname)
            }
        })
    });

    routes.post(`api/upload-image`, upload.single('photo'), async (req: any, res: Response) => {
        
       return res.json({message:'Image uploaded successfully',uploadLocation:req.file.location});
    });

这篇关于如何使用multer获取s3存储桶上传的对象url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆