3

I tried few methods, not able to get it working.

Client side(React), I am sending a zip file as follows using POST,

const data = new FormData();
        data.append('file', file);
        data.append('filename', file.name);

let params = {
            headers: {
                'Content-Type': 'multipart/form-data'
            },
            body: data
        };

Server side:(API Gateway/Lambda/Nodejs) I added 'multipart/form-data' to Binary Media Type on Gateway side.

When parsing through lambda event.body is not well formed. It looks like this:

{"body": "e30=",
"isBase64Encoded": true }

Any ideas what might be happening? Any takes on how to parse?

user3900196
  • 457
  • 2
  • 6
  • 18
  • Server side:(API Gateway/Lambda/Nodejs) I added 'multipart/form-data' to Binary Media Type on Gateway side. Can you paste a sample of this? For better clarification. – Gurpreet Singh Drish Jun 16 '19 at 07:03

2 Answers2

4

Although Ariz's answer is correct, I strongly recommend you to look into AWS Pre-Signed Upload URLs. It allows your clients to upload the file first to an AWS S3 Bucket, from where your lambda function can later access the object.

Especially when you're working with large binary files, the former approach can lead to a lot of problems (-> memory issues, which is sparse in Lambda).

I have written a short blog post about this in the past.

ampunix
  • 123
  • 2
  • 9
  • I definitely considered this approach. Only problem with this is exposing S3 operations on client side. I feel more comfortable handling these kind of stuff behind API Gateway. – user3900196 Feb 08 '19 at 02:55
  • The Pre-Signed Upload URLs were intended by AWS to be used client side (from the browser e.g.). You just have to generate the Pre-Signed URL behind the API Gateway. Then you have nothing to worry. – ampunix Feb 08 '19 at 03:51
  • ahh got it! And then i run into this error 'Signature does not match'. see this link https://stackoverflow.com/questions/54660048/s3-presigned-url-multipart-formdata-upload-errsignature-does-not-match – user3900196 Feb 12 '19 at 23:11
  • Specify the file as body in the request, not as form-data. 2. – ampunix Feb 13 '19 at 13:28
2

you are getting base64 encoded data, following is one of the ways to decode. However it's an empty object.

var base64 = 'e30='
var decodedData = Buffer.from(base64, 'base64').toString();

console.log(decodedData)
AZ_
  • 3,094
  • 1
  • 9
  • 19
  • Thanks! I figured it out. I was just trying to handle it as a buffer. Did not occur to convert that to string. Currently i am having trouble parsing the data. I am using 'parse-multipart'. It doesn't work or may be i am doing something different. – user3900196 Feb 06 '19 at 21:37