title: Node js - Moving a file from /tmp in Amazon Lambda to an s3bucket
body: <p>I am trying to write a simple screenshot program in amazon lambda which will take a screengrab of a given url in the following JSON format: </p>
  
  <pre><code>{
    "site": "www.google.com",
    "width": "320",
    "height": "480"
  }
  </code></pre>
  
  <p>The code for my lambda function is as below:</p>
  
  <pre><code>'use strict';
  
  var AWS = require('aws-sdk');
  var fs = require('fs');
  var webshot = require('webshot');
  var request = require('request');
  
  var s3bucket = '&lt;bucket-name&gt;';
  exports.handler = function(event, context, callback) {
    console.log( "Attempting to screenshot:", event.site);
  
    var parts = event.site.split('?'),
        base64Key = new Buffer(parts[0].toLowerCase()).toString('base64').replace(/\//g, '|').replace(/\+/g, '$') + '.png',
        options = {
          shotSize: {
            width: event.width,
            height: event.height
          }
        },
        s3 = new AWS.S3(),
        renderStream = webshot(event.site, options),
        file = fs.createWriteStream('/tmp/' + base64Key, {encoding: 'binary'}),
        bin = '';
  
    renderStream.on('data', function(data) {
      // bin += data.toString('binary');
      file.write(data.toString('binary'), 'binary');
    });
  
    renderStream.on('end', function() {
      // var options = {
      //    Bucket: s3bucket,
      //    Key: base64Key,
      //    ACL: 'public-read',
      //    body: file
      // };
      // s3.putObject(options, function(err, data) {
      //     if(err) {
      //       callback(err);
      //     } else {
      //       callback(null, 'Image uploaded to Amazon s3 bucket: ' + s3bucket);
      //     }
      // });
  
  
      var buffer = new Buffer(bin);
      s3.putObject({
          Bucket: s3bucket,
          Key: base64Key,
          ACL: 'public-read',
          Body: buffer.toString('binary')
      }, function(err, data) {
         if(err) {
           callback(err);
         } else {
           callback(null, 'Image uploaded to Amazon s3 bucket: ' + s3bucket);
         }
      });
    });
  }
  </code></pre>
  
  <p><strong>My problem is this:</strong> </p>
  
  <p>When I write the file to the /tmp directory it is there as expected, however, when I then try and upload the image from the /tmp directory to my s3 bucket it is not working correctly. </p>
  
  <p>I have tried to upload the raw data (bin), and also within a buffer as commented out above. I have also tried to upload the file stream directly, though these attempts did not work. </p>
  
  <p>When running on amazon the log files are not showing any errors. I have looked through the <a href="http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property" rel="nofollow">Docs</a> on putObject though I cannot seem to see where I am going wrong. </p>
  
  <p>Any help on this would be greatly appreciated. </p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>The problem appears to be that the file is created in /tmp but no data is written to it so when it is copied over to S3 there is a .png file with size 0 bytes</p>