Introduce
In Lambda, the payload for request, response is limited to 6mb.
So when someone download a file larger than 6mb. it gives and error.
Working with presigned URLs
Normally, access a link to a file in S3, it will download.
So I’m going to use the presigend url.
Code
This is an example of using the AWS-SDK to get a get sigend url.
I wanted to change the file name on download and use a ResponseContentDisposition.
One thing to check here is that you need to encode file name.
const S3 = new AWS.S3({
region: this.configService.get('AWS_DEFAULT_REGION'),
credentials: {
accessKeyId: this.configService.get('AWS_ACCESS_KEY_ID'),
secretAccessKey: this.configService.get('AWS_SECRET_ACCESS_KEY'),
},
});
const encodedFilename = encodeURIComponent(file.title);
const url = await S3.getSignedUrl('getObject', {
Bucket: this.configService.get('AWS_PRIVATE_BUCKET'),
Key: file.path,
Expires: 60,
ResponseContentDisposition: 'attachment; filename="' + encodedFilename + '"',
});
The example above uses the AWS-SDK and was prompted to switch to the v3 version.
So I rewrote the code to use the v3 version.
const s3 = new S3Client({
region: this.configService.get('AWS_REGION'),
credentials: {
accessKeyId: this.configService.get('AWS_KEY'),
secretAccessKey: this.configService.get('AWS_SECRET'),
},
});
const encodedFilename = encodeURIComponent(file.title);
const command = new GetObjectCommand({
Bucket: this.configService.get('AWS_BUCKET'),
Key: file.path,
ResponseContentDisposition:
'attachment; filename="' + encodedFilename + '"',
});
const url = await getSignedUrl(s3, command, { expiresIn: 60 });