0

此代码已成功在 s3 上上传对象,但我无法弄清楚如何获取上传的对象 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) => {
        
        res.send('Image uploaded successfully');
    });

有没有办法或回调来获取网址?

4

1 回答 1

0
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});
    });
于 2021-07-16T09:35:25.590 回答