title: Corrupt/truncated mp4 upload to S3 bucket using NodeJS and AWS S3
body: <p>I'm using fs.readFile to read in a local mp4 file of around 700KB, and AWS SDK to load the file onto my S3 bucket. The process works, but the file is corrupt, presumably truncated because the size of the resulting file on S3 is only around 500KB. </p>
  
  <p>The code I'm using works for images just fine. Just not working for mp4 files. Here's my code:</p>
  
  <pre><code>fs.readFile(file_location, function (err, data) {
      if (err) { 
          console.log('fs error': err);
      } else {
          var base64data = new Buffer(data, 'binary');
  
          var params = {
              Bucket: config.settings.bucket, 
              Key: 'file.mp4', 
              Body: base64data,
              ContentEncoding: 'base64',
              ContentType: 'video/mp4'
          };
  
          s3.putObject(params, function(err, data) {
              if (err) { 
                  console.log('Error putting object on S3: ', err); 
              } else { 
                  console.log('Placed object on S3: ', object_key); 
              }  
          });
      }
  });
  </code></pre>
  
  <p>I've tried omitting the ContentEncoding and/or the ContentType properties from the params object.</p>
  
  <p>EDIT:</p>
  
  <p>So when I did a console.log of the length of the data that fs.readFile returned, it consistently comes up short. I didn't realize I wasn't actually handling base64 data, either. So if I use the following code where I ask to read the file in using base64, the data.length from fs.readFile appears to be correct, and an mp4 file shows up on my S3 bucket with a playhead in the browser (which I wasn't getting before), but the video won't play &amp; it's black, so it appears to still be corrupt.</p>
  
  <pre><code>fs.readFile(file_location, 'base64', function (err, data) {
      if (err) { 
          console.log('fs error': err);
      } else {
          var params = {
              Bucket: config.settings.bucket, 
              Key: 'file.mp4', 
              Body: data,
              ContentEncoding: 'base64',
              ContentType: 'video/mp4'
          };
  
          s3.putObject(params, function(err, data) {
              if (err) { 
                  console.log('Error putting object on S3: ', err); 
              } else { 
                  console.log('Placed object on S3: ', object_key); 
              }  
          });
      }
  });
  </code></pre>