export const downloadFiles3 = async (req: Request, res: Response) => {
const key = req.params.key;
try {
const getObjectParams = {
Bucket: process.env.AWS_BUCKET || '',
Key: key,
};
const data = await s3.getObject(getObjectParams).promise();
res.setHeader('Content-Type', 'application/zip');
res.setHeader('Content-Disposition', `attachment; filename=${key}`);
res.send(data.Body);
console.log('File sent successfully.');
} catch (error) {
if (!res.headersSent) {
if (error instanceof Error) {
console.log('Error retrieving file content:', error);
res.status(500).json({
message: 'Error retrieving file content',
error: error.message,
});
} else {
console.log('Unexpected error:', error);
res.status(500).json({
message: 'Unexpected error',
error: String(error),
});
}
} else {
console.log('Response headers already sent:', error);
}
}
};
I tried to stream the response , it also worked locally but not when i deployed on aws. I am getting no errors in aws cloudwatch.
the zipfile being downloaded is corrupt