4

I am trying to send a PDF or text file to an AWS Lambda function (Node JS). I'd like to be able to process this file in the lambda function. I know often the best practice is to use a trigger function from an s3 bucket. However, I'd like to be able to send this lambda function a file from formdata, extract information from the file and then return the extracted info back.

I have been able to first encode the file in 64 bit binary data and send it to AWS lambda via a JSON, but often when I try to decode the file (especially a PDF) in the Lambda Function it is corrupted or empty.

Image files seem to work well for this type of encoding, but been unsuccessful with PDFs. Any help greatly appreciated. Rather then encode in base 64 is there a way I can obtain the file from a formdata? My code is below:

export const handler = async(event) => {
   
    console.log("event", event)
    
    var converted = atob(event.body) // RATHER HOW WOULD I READ A FILE FROM FORMDATA



     const response =  {
        "statusCode": 200,
        "headers": {"Content-Type":"text/html", //"application/pdf", // "multipart/form-data", //
            "Access-Control-Allow-Origin":"*",
            "Access-Control-Allow-Headers":"Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token"
                        
        },
        "body": event,
        "isBase64Encoded": true
        }
        
    return response;
};

thanks so much

0

2 Answers 2

6

I assume that you are using an API gateway to trigger the lambda function. In that case, you have to enable multipart/form-data in API gateway as mentioned in the documentation. The gist of the documentation is as follows:

  1. In the Settings pane, choose Add Binary Media Type in the Binary Media Types section. Type a required media type, for example, image/png, in the input text field.
  2. Add Content-Type and Accept to the request headers for your proxy method.
  3. Add those same headers to the integration request headers.
  4. Deploy the API

PS: If you are using Lambda Proxy integration ({proxy+}, just steps 1 and 4 are enough.

Sign up to request clarification or add additional context in comments.

Comments

0

I recently had a requirement where I had to upload a csv, do some processing on it and then return back the file.

Lambda will take the file, encode in base64 format and then decode that for you in req.body

Here is a simple code that you can use to build upon, it uses lambda-api package to handle the routing.

import createAPI from 'lambda-api';

// instantiate 
const api = createAPI();

api.get('/status', async (req, res) => {
  return { status: 'ok' };
});

api.post('/upload', async (req, res) => {
  console.log(req.body)
  console.log(typeof req.body);

  return { status: 'upload working' };
})

export const handler = async(event, context) => {
  return await api.run(event, context);
}

For me if I send the binary file via postman or via form-data it was coming in the req.body as a string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.