title: Upload a binary file to S3 using AWS SDK for Node.js
body: <p><strong>Update:</strong> For future reference, Amazon have now updated the documentation from what was there at time of asking. As per @Loren Segal's comment below:-</p>
  
  <blockquote>
    <p>We've corrected the docs in the latest preview release to document this parameter properly. Sorry about the mixup! </p>
  </blockquote>
  
  <hr>
  
  <p>I'm trying out the developer preview of <a href="http://aws.amazon.com/about-aws/whats-new/2012/12/04/introducing-the-aws-sdk-for-node-js/" rel="noreferrer">the AWS SDK for Node.Js</a> and want to upload a zipped tarball to S3 using <code>putObject</code>.</p>
  
  <p>According to <a href="http://docs.amazonwebservices.com/AWSJavaScriptSDK/latest/AWS/S3/Client.html#putObject-property" rel="noreferrer">the documentation</a>, the <code>Body</code> parameter should be...</p>
  
  <blockquote>
    <p>Body - (Base64 Encoded Data)</p>
  </blockquote>
  
  <p>...therefore, I'm trying out the following code...</p>
  
  <pre><code>var AWS = require('aws-sdk'),
      fs = require('fs');
  
  // For dev purposes only
  AWS.config.update({ accessKeyId: 'key', secretAccessKey: 'secret' });
  
  // Read in the file, convert it to base64, store to S3
  fs.readFile('myarchive.tgz', function (err, data) {
    if (err) { throw err; }
  
    var base64data = new Buffer(data, 'binary').toString('base64');
  
    var s3 = new AWS.S3();
    s3.client.putObject({
      Bucket: 'mybucketname',
      Key: 'myarchive.tgz',
      Body: base64data
    }).done(function (resp) {
      console.log('Successfully uploaded package.');
    });
  
  });
  </code></pre>
  
  <p>Whilst I can then see the file in S3, if I download it and attempt to decompress it I get an error that the file is corrupted. Therefore it seems that my method for 'base64 encoded data' is off.</p>
  
  <p>Can someone please help me to upload a binary file using <code>putObject</code>?</p>