title: Node base64 upload to AWS S3 bucket makes image broken
body: <p>I have a node route that is supposed to upload an image on an AWS S3 bucket, based on a data-uri. Everything works properly, except that the image is broken or not fully uploaded.</p>
  
  <p>Here's my node : </p>
  
  <pre><code>router.get('/share', function(req, res, next){
  
      // Getting the base64 uri from the queried url
      var base64 = req.query.imgUrl;
  
      // Getting the file type, ie: jpeg, png or gif
      const type = base64.split(';')[0].split('/')[1];
  
      // Creating a unique key for S3
      const key = uuidv1();
  
      const buf = new Buffer(base64.replace(/^data:image\/\w+;base64,/, ""), 'base64');
      var params = {
          Bucket: 'myBucket',
          Key: key,
          Body: buf,
          ContentEncoding: 'base64',
          ContentType: `image/${type}`,
          ContentLength: buf.length,
          ACL: 'public-read'
      };
  
      // Uploading
      s3.putObject(params, function(err, data){
          if (err) { 
              console.log(err);
              console.log('Error uploading data: ', data);
          } else {
              console.log('succesfully uploaded the image!');
          }
      });
  
      res.send('foo');
  });
  </code></pre>
  
  <p>The image I get on S3: </p>
  
  <p><a href="https://i.stack.imgur.com/1oHYq.jpg" rel="nofollow noreferrer">What a mess...</a></p>
  
  <p>The image it's supposed to be : </p>
  
  <p><a href="https://i.stack.imgur.com/Ug87i.jpg" rel="nofollow noreferrer">A magnificent dickbutt</a></p>