4

I want to upload multipart/form-data file to S3 using Nodejs.

I have tried various approaches but none of them are working. I was able to write content to S3 from lambda but when the file is downloaded from S3, it was corrupted.

Can someone provide me a working example or steps that could help me?

Thanking you in anticipation.

Please suggest another alternative if you think so.

Following is my lambda code:

export const uploadFile = async event => {
  const parser = require("lambda-multipart-parser");
  const result = await parser.parse(event);
  const { content, filename, contentType } = result.files[0];
  const params = {
    Bucket: "name-of-the-bucket",
    Key: filename,
    Body: content,
    ContentDisposition: `attachment; filename="${filename}";`,
    ContentType: contentType,
    ACL: "public-read"
  };

  const res = await s3.upload(params).promise();
  return {
        statusCode: 200,
        body: JSON.stringify({
            docUrl: res.Location
        })
    };
}
4
  • 1
    Show your code, otherwise we can't help you. Commented Jan 24, 2020 at 17:07
  • @MarcosCasagrande I have added the code... Commented Jan 25, 2020 at 1:47
  • 1
    I've faced a similar issue, ultimately, your best bet would probably changed the file to Base64 string and send over to Lambda and convert it back to file before you upload to S3. Read more here: medium.com/@olotintemitope/… Commented Jan 25, 2020 at 2:18
  • I need a file upload for various file types. png, jpg, pdf, doc,docx, .xls, csv and so on. Commented Jan 25, 2020 at 5:49

2 Answers 2

8

If want to upload file through lambda, one way is to open your AWS API Gateway console.

Go to

"API" -> {YourAPI} -> "Settings"

There you will find "Binary Media Types" section.

Add following media type:

multipart/form-data

Save your changes.

Then Go to "Resources" -> "proxy method"(eg. "ANY") -> "Method Request" -> "HTTP Request Headers" and add following headers "Content-Type", "Accept".

Finally deploy your api.

For more info visit: https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-payload-encodings-configure-with-console.html

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

1 Comment

Oof. This. I forgot to hit "Deploy API" after modifying the Resources and Binary Media Types.. ugh.
1

There are 2 possible points of failure - Lambda receives corrupted data or you corrupt data while sending it to S3.

Sending multipart/form-data content to Lambda is not straightforward. You can see how to do that here.
After you did this and you're sure your data is correct in Lambda, check if you send it to S3 correctly (see S3 docs and examples for that).

2 Comments

There are two things. One I need to setup Binary Media Types on AWS gateway. and second parser.
@KedarShinde can you tell me more about parser

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.