从 Express JS 服务器中的 s3 获取对象并将其作为响应发送

问题描述 投票:0回答:1

根据客户的授权请求,我想将所有文件发回文件响应。在这里,我从

S3
存储桶获取文件。从
S3
发回文件并将其发送给客户的方法是什么?

const s3 = new S3Client({...})

router.get('/getfile', (req, res) => {
  const data = await s3.send(
    new GetObjectCommand({
      Bucket: 'bucket',
      Key: 'path',
    })
  )

  // an efficient way to send back the file
  // res.send(fileStream)
})

如何发回数据?

javascript express amazon-s3
1个回答
0
投票

使用

pipe()
将流推送至响应。

const { Body, ContentType, ContentDisposition } = await s3.send(command);

res.setHeader('Content-Type', ContentType);
res.setHeader('Content-Disposition', ContentDisposition || `attachment; filename="untitled"`);

Body.pipe(res);
© www.soinside.com 2019 - 2024. All rights reserved.