2
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

3
  • Could you provide us more details on 1). The memory size allotted to your Lambda function 2). The chip architecture of your development machine and 3). The chip architecture assigned to the Lambda running this code?
    – Allan Chua
    Commented May 29 at 15:18
  • Where is your Lambda handler code?
    – stdunbar
    Commented May 30 at 1:15
  • @AllanChua my development machine is x64. As for the lambda function architecture is x86_64, memory size is 512 mb, timeout is 1 min the zip files that i am trying to download from s3 bucket are in kbs not even mbs Commented May 30 at 6:54

0