Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to read a file from form-Data in an AWS Lambda function

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

like image 423
Katie Melosto Avatar asked Oct 24 '25 14:10

Katie Melosto


1 Answers

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.

like image 103
user11666461 Avatar answered Oct 26 '25 03:10

user11666461



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!