Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload image to AWS S3 bucket using Node. JS

I'm trying to upload an image to my S3 bucket from a webpage using node.js. I am able to create a folder, but uploading the image file to that folder is not working for me. Here are my dependencies in my package.json

"dependencies": {
    "aws-sdk": "^2.331.0",
    "bcrypt-nodejs": "0.0.3",
    "busboy": "^0.2.14",
    "busboy-body-parser": "^0.3.2",
    "connect-busboy": "0.0.2",
    "ejs": "^2.6.1",
    "express": "^4.16.3",
    "express-handlebars": "^3.0.0",
    "express-session": "^1.15.6",
    "mysql": "^2.16.0",
    "mysql2": "^1.6.1",
    "passport": "^0.4.0",
    "passport-local-sequelize": "^0.8.0",
    "path": "^0.12.7",
    "sequelize": "^4.39.0"
  }

and this is the part of my api_routes.js file that I'm working with

const bucketName = 'socialmemedia';
const bucketRegion = 'us-east-1';

AWS.config.update({
  region: bucketRegion
});
AWS.config.apiVersions = {
  s3: '2006-03-01',
};

const s3 = new AWS.S3();

module.exports = function (app) {
 // upload image to S3
  app.post("/api/upload", function (req, res) {
    const file = (req.body.imageUpload);
    const busboy = new Busboy({
      headers: req.headers
    });
    busboy.on('finish', function () {
      console.log('Upload finished');
      console.log(file);
      uploadToS3(file);
    });
     req.pipe(busboy);
  });


  function uploadToS3(file) {
    // console.log(req.body);

    const folder = (req.user.username + "/")
    console.log("Folder name: " + folder);
    const params = {
      Bucket: bucketName,
      Key: folder,
      ACL: 'public-read',
      Body: stream
    };

    s3.upload(params, function (err, data) {
      if (err) {
        console.log("Error: ", err);
      } else {
        console.log("Successfully created a folder on S3");
        
      }
    });
    res.redirect("/feed");
  }

};

and here is the form snippet

<form action="/api/upload" method="POST" class="image-form">
        <input id="image-upload" class="file-add" type="file" accept="image/*" name="imageUpload"/>
        <button type="submit" id="image-upload" class="sinsup-button">Upload Image</button>
</form>

any idea on how to get the image to actually upload into the S3 bucket?

like image 504
Em M Avatar asked Oct 13 '18 16:10

Em M


People also ask

How do I upload pictures to AWS S3?

jpg . Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that you want to upload your folders or files to. Choose Upload.


1 Answers

I figured it out, thanks for your help @me. I was using s3.upload instead of s3.putObject. Answering to help anyone else who may have this problem in the future.

  // upload image to S3
  app.post("/api/upload", function (req, res) {
    const folder = (req.user.username + "/");
    const file = (req.body.imageUpload);
    const params = {
      Bucket: bucketName,
      Key: (folder + file),
      ACL: 'public-read',
      Body: file
    };
    console.log("Folder name: " + folder);
    console.log("File: " + file);
    

    s3.putObject(params, function (err, data) {
      if (err) {
        console.log("Error: ", err);
      } else {
        console.log(data);
      }
    });
    res.redirect("/feed");
  });
like image 152
Em M Avatar answered Sep 22 '22 07:09

Em M