Search code examples
amazon-web-servicesamazon-s3multipartform-dataserverless-framework

blank pdf files at S3 when uploading with Serverless framework API Gateway


I am uploading pdf file to S3 with this function via postman/multipart file upload.

  public async upload(folder: string, fileName: string, content: any, language: string): Promise<void> {
   try {
     const bucketParams = {
       Bucket: process.env.BUCKET_NAME,
       Key: common.buildObjectKey(folder, fileName, language),
       Body: Buffer.from(content),
  };

  await s3Client.send(new PutObjectCommand(bucketParams));
} catch (error) {
  console.log("Upload failed. Error: ", error);
 }
}

content argument comes from my Lambda function event which is multipart file

const multipart = require("aws-lambda-multipart-parser");

export const handler: Handler = async (event) => {
  try {
    const { folder, fileName, language } = event.pathParameters;
    const parsedEvent = multipart.parse(event, true);
    ....

When I run this code from Serverless Offline i.e. locally, it works, however, the deployed code at AWS doesn't work. here are the file size differences.

enter image description here

at serverless.yml I have as it's described here at aws-lambda-multipart-parser npm package page

  apiGateway:
    binaryMediaTypes:
      - 'multipart/form-data'

Any idea what am I doing wrong?


Solution

  • I had the same problem and had to add the following:

    apigwBinary:
    types:
      - 'multipart/form-data'
      - 'application/pdf'