Ravi Doyenhub
Ravi Doyenhub

Reputation: 63

File getting corrupted on S3 when upload using serverless framework

I am using Serverless Framework which has functionality of the File(Image/Excel) upload on S3 bucket of AWS. While uploading file I am using S3 service of "putObject" function with basic params for upload. Data of the file is being passed in Binary String format and gets uploaded on S3 bucket.

My Local system uploads the file on S3 bucket perfectly as per requirement but when serverless framework is deployed using the "Lambda Function" of AWS for the same function a corrupted file gets uploaded.

Below given is my demo code:

//Router
app.post('/api/fileUpload', controller.fileUpload);

//Controller
exports.fileUpload = (req, res) => {
let params = {
    Bucket: 'BucketName',
    Key: 'keyofAWSS3',
    ContentType: 'image/jpeg',
    Body: data,//Data of file to be uploaded in Binary String format
    ACL: 'private'
};

//to create file on S3
return s3Service.CreateToS3(params).then((fromResolve) => {
  console.log(fromResolve);
}).catch((error) => {
   console.log(error);
}); 

}

//Service
exports.CreateToS3 = (params) => {
  let s3 = new AWS.S3();

return new Promise((resolve, reject) => {

    //Upload as a file to S3
    s3.putObject(params, (err, data) => {

    if (err) {
        reject(err);
    }

    resolve(data);

      });
    });
 };

Upvotes: 4

Views: 2282

Answers (2)

Binh Ho
Binh Ho

Reputation: 4993

Add this to serverless.yml.

provider:
  apiGateway:
    binaryMediaTypes:
      - '*/*'

See https://stackoverflow.com/a/61003498/9585130

Hope this help!

Upvotes: 1

Craig Fisher
Craig Fisher

Reputation: 1802

Make sure you have configured your API Gateway to allow binary media to be passed through.

Upvotes: 2

Related Questions