Search code examples
amazon-s3aws-lambdaamazon-cloudfront

Downloading zipped CSV file through CloudFront


I have a lambda function that when called will unload() from a redshift database to an s3 bucket using the following commands:

unload ('<SQL QUERY TO UNLOAD')
to 's3://<BUCKET
iam_role 'arn:aws:iam::<ROLEID>
delimiter ','
PARALLEL OFF
ESCAPE
ALLOWOVERWRITE
GZIP

I'm then running the following nodejs code after that operation completes in the attempt to add a ContentType and ContentEncoding headers:

s3.copyObject({
     Bucket: directory,
     MetadataDirective: 'REPLACE',
     CopySource: `${directory}/${files[i].Key}`,
     ContentType: 'text/csv',
     ContentEncoding: 'gzip',
     Key: `${files[i].Key}`
}).promise()

Finally, when that's complete, I email a CloudFront link to the user who requested the data. When a user taps the link, they download a .gz file. I'm expecting that when the user unzips the file that they find a .csv file HOWEVER, they find a file without a file extension...

Is there a way to return a zipped csv file through CloudFront (or even through s3 if CloudFront isn't an option).

As it stands, users have to unzip and then add the .csv file extension every time they download my data which is less than ideal.


Solution

  • Georgeawg helped me realized I could just use s3's copyObject function to rename the file to csv.gz. I then run removeObject to delete the old file and return the csv.gz file. It unzips correctly.