title: How to upload a file directly to Amazon Cloud Front using Node.js
body: <p>I just configured an Amazon Cloud Front distribution, and connected it to an existent Amazon S3 bucket. Having said that, this bucket holds 10GB to 40GB video files, and I want to use CF to make the upload process faster (also the download, but the upload is more important in this case).</p>
  
  <p>Finally, I created a simple JS script to push a file to this bucket connected to CF:</p>
  
  <pre><code>var AWS_ACCESS_KEY_ID = "111",
  AWS_SECRET_ACCESS_KEY = "222",
  S3_BUCKET = "videos.company.com",
  S3_KEY = "remote_filename.mkv",
  fs = require('fs'),
  AWS = require('aws-sdk');
  
  AWS.config.update({accessKeyId: AWS_ACCESS_KEY_ID, secretAccessKey: AWS_SECRET_ACCESS_KEY});
  
  var startDate = new Date().toUTCString();
  
  var fileStream = fs.createReadStream(__dirname + '/test.mkv');
  fileStream.on('error', function (err) {
    if (err) { throw err; }
  });  
  fileStream.on('open', function () {
    var s3 = new AWS.S3();
    s3.putObject({
      Bucket: S3_BUCKET,
      Key: S3_KEY,
      Body: fileStream
    }, function(err) {
      if (err) {
        console.log(err, err.stack);
      } else {
        console.log('Uploaded file to ' + S3_KEY + ' at ' + new Date().toUTCString());
        console.log("Start time: " + startDate + ", end time: " + new Date().toUTCString());
      }
    }).on('httpUploadProgress', function(progress) {
      console.log('Upload', progress.loaded, 'of' , progress.total, 'bytes' + " --&gt; " + ((progress.loaded*100)/progress.total).toFixed(2) + "%");
    });
  });
  </code></pre>
  
  <p>After running the script, I did not notice any speed changes (upload with and without CF distribution)... actually the upload time for both cases are exactly the same. However, after reading some forums, I saw people talking about doing a POST request using the CF url.</p>
  
  <p>How can I upload a file from my server to CF directly?</p>
  
  <p>Thanks!</p>