title: How can I upload a file from an HTTP response body to S3 using putObject() from the AWS SDK (in Node.js)?
body: <p>I'm trying to save a PDF file into S3 with the AWS SDK.
  I'm getting the PDF through the body of a POST request (<code>Application/PDF</code>).</p>
  
  <p>When saving the file into the local HD with <code>fs.writeFile</code>, the file looks ok. But when uploading it to S3, the file is corrupted (it's just a single 
   page PDF).</p>
  
  <p>Any help or hint would be greatly appreciated!</p>
  
  <pre class="lang-js prettyprint-override"><code>var data = body // body from a POST request.
  var fileName = "test.pdf";
  
  fs.writeFile(fileName, data, {encoding : "binary"}, function(err, data) {
      console.log('saved'); // File is OK!
  });
  
  s3.putObject({ Bucket: "bucketName", Key: fileName, Body: data }, function(err, data) {
      console.log('uploaded') // File uploads incorrectly.
  });
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>It works if I write and then read the file and then upload it.</p>
  
  <pre class="lang-js prettyprint-override"><code>fs.writeFile(fileName, data, {encoding : "binary"}, function(err, data) {
      fs.readFile(fileName, function(err, fileData) {
          s3.putObject({ Bucket: "bucketName", Key: fileName, Body: fileData }, function(err, data) {
              console.log('uploaded') // File uploads correctly.
          });
      });
  });
  </code></pre>