根据客户的授权请求,我想将所有文件发回文件响应。在这里,我从
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)
})
如何发回数据?
使用
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);