3

I'm using serverless-http to make an express endpoint on AWS Lambda - pretty simple in general. The flow is basically:

  1. POST a zip file via a multipart form to my endpoint
  2. Unzip the file (which contains a bunch of excel files)
  3. Merge the files into a single Excel file
  4. res.sendFile(file) the file back to the user

I'm not stuck on this flow 100%, but that's the gist of what I'm trying to do.

Lambda functions SHOULD give me access to /tmp for storage, so I've tried messing around with Multer to store files there and then read the contents, I've also tried the decompress-zip library and it seems like the files never "work". I've even tried just uploading an image and immediately sending it back. It sends back an files called incoming.[extension], but it's always corrupt. Am I missing something? Is there a better way to do this?

McB
  • 1,082
  • 1
  • 18
  • 36
  • 2
    I advice you to avoid as much as possible to use the disc on Lambdas. Try to make all the process in memory, check: https://stackoverflow.com/questions/10359485/how-to-download-and-unzip-a-zip-file-in-memory-in-nodejs. – RoberMP Mar 23 '19 at 07:10

1 Answers1

2

Typically when working with files the approach is to use S3 as the storage, and there are a few reasons for it, but one of the most important is the fact that Lambda has an event size limit of 6mb, so you can't easily POST a huge file directly to it.

If your zipped excel files is always going to be less than that, then you are safe on that regard. If not, then you should look into a different flow, maybe something using AWS step functions with Lambda and S3.

Concerning your issue with unzipping the file, I have personally used and can recommend adm-zip, which would look something like this:

//unzip and extract file entries
var zip = new AdmZip(rawZipData);
var zipEntries = zip.getEntries();
console.log("Zip contents : " + zipEntries.toString());
zipEntries.forEach(function(entry){
    var fileContent = entry.getData().toString("utf8");
});
Deiv
  • 3,000
  • 2
  • 18
  • 30
  • Thanks for this! I didn't realize there was a 6MB event limit - wouldn't have been an issue during initial testing, but we could definitely exceed that eventually. I've set up a step function and will see if AdmZip can extract my files from S3. Cheers! – McB Mar 26 '19 at 15:49