Student
Student

Reputation: 41

Upload image from html input to S3 bucket

I am trying to create a webpage where a person can upload an image and that image is stored in S3 bucket. Currently, I am getting this error: "Unsupported body payload object" while uploading to S3 bucket.

Here is my relevant HTML:

<form enctype="multipart/form-data" action="/editprofile" method="post">
   <input type="file" value="Change Profile Picture" name="profilePicture" id="profilePicture" >
</form>

Here is my relevant JS code:

//in app.js
const fileupload = require("express-fileupload");
app.use(fileupload());
app.post('/editprofile', function(req, res) {
  routes.editprofile(req, res);
});

//in routes.js in editprofile function
db.putInBucket("bucket", "key" + ".png", req.files.profilePicture);

//in db in putInBucket
var putInBucket = function (bucket, key, file) {
  var upload = new AWS.S3.ManagedUpload({
    params: {
      Bucket: bucket,
      Key: key,
      Body: file
    }
  });

  var promise = upload.promise();
  promise.catch(function(error) {
    console.log("Error in uploading to S3" + error);
  });
};

Upvotes: 1

Views: 1700

Answers (1)

Raj Paliwal
Raj Paliwal

Reputation: 947

You need to convert content of req.files.profilePicture into binary before uploading to S3.

var binaryProfilePicture = new Buffer(req.files.profilePicture, "binary");

db.putInBucket("bucket", "key" + ".png", binaryProfilePicture );

Upvotes: 3

Related Questions