Upload image to s3, image broken

I upload image to s3 through lambda function.
I used sls offline start to upload, everything is fine, I could see the image that I uploaded.
But when I deploy it to aws and call api, my image was uploaded but couldn’t be seen. After I downloaded image, I also couldn’t open my picture because it was damaged.

Code uploaded:

const lstData = Promise.all(files.map(async file_obj => {

let fileExtension = file_obj.filename.split('.').pop()
        const actionId = uuid.v4()
        let buf = new Buffer(file_obj.content, 'binary')
        // console.log(file_obj.content)
        const s3Params = {
            Bucket: process.env.AWS_BUCKET,
            Key: `${actionId}.${fileExtension}`,
            ACL: 'public-read',
            ContentType: `${file_obj.contentType}`,
            Body: buf
        }
        let data = await s3.upload(s3Params).promise()

        return data

}}

Solution I tried:

  • add multipart/form-data in Api Gateway
2 Likes

adding the below worked for me:

provider:
  apiGateway:
    binaryMediaTypes:
      - 'image/png'
      - ‘image/jpeg’
      - 'multipart/form-data'
1 Like