Prompt Detail:
If you add .xlsx
to the key when trying to retrieve the file from S3, you will get an error indicating that the key does not exist. This is because the key in S3 does not include the file extension.
To fix this, you can remove the .xlsx
from the key parameter when calling the getObject
method. Here's an updated code snippet:
javascriptconst AWS = require('aws-sdk');
const s3 = new AWS.S3();
fastify.get('/download/:uuid', async (req, res) => {
const uuid = req.params.uuid;
const params = {
Bucket: 'your-bucket-name',
Key: `${uuid}`
};
try {
const data = await s3.getObject(params).promise();
res.header('Content-Disposition', `attachment; filename=your-chosen-name.xlsx`);
res.header('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
res.send(data.Body);
} catch (error) {
console.error(error);
res.send(error);
}
});
In this updated code, the key parameter passed to getObject
method no longer includes the .xlsx
extension.
Add a comment