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);
});
});
};