title: Upload image to AWS S3 bucket using Node. JS
body: <p>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</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>"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"
    }</code></pre>
  </div>
  </div>
  </p>
  
  <p>and this is the part of my api_routes.js file that I'm working with</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>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");
    }
  
  };</code></pre>
  </div>
  </div>
  </p>
  
  <p>and here is the form snippet </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;form action="/api/upload" method="POST" class="image-form"&gt;
          &lt;input id="image-upload" class="file-add" type="file" accept="image/*" name="imageUpload"/&gt;
          &lt;button type="submit" id="image-upload" class="sinsup-button"&gt;Upload Image&lt;/button&gt;
  &lt;/form&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>any idea on how to get the image to actually upload into the S3 bucket?</p>