text: var s3 = require('s3'),
          fs = require('fs'),
          RSVP = require('rsvp'),
          dir = require('node-dir');
  
  
  
  var client = s3.createClient({
      maxAsyncS3: 5, // this is the default 
      s3RetryCount: 3, // this is the default 
      s3RetryDelay: 1000, // this is the default 
      multipartUploadThreshold: 20971520, // this is the default (20 MB) 
      multipartUploadSize: 15728640, // this is the default (15 MB) 
      s3Options: {
          accessKeyId: "",
          secretAccessKey: "",
          region: 'ap-south-1',
          // any other options are passed to new AWS.S3() 
          // See: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Config.html#constructor-property 
      },
  });
  
  
  var date = new Date();
  var nextYear = date.setFullYear(date.getFullYear() + 1);
  
  
  
  /*
   * @param
   * file : file path => \xampp\htdocs\test_aws\images\large\StripAd_GIF.gif
   * expires : date time : Wed Jul 27 2016 14:41:22 GMT+0530 (Sri Lanka Standard Time)
   */
  function uploader(file, expires) {
      console.log(expires)
      var params = {
          localFile: file,
          s3Params: {
              Bucket: "rgfiles1",
              Key: "testrg/images/large/" + file,
              Expires: expires
  
                      // other options supported by getObject 
                      // See: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#getObject-property 
          },
      };
  
  
      var uploader = client.uploadFile(params);
      uploader.on('error', function (err) {
          console.error("unable to upload:", err.stack);
      });
      uploader.on('progress', function () {
          console.log("progress", uploader.progressMd5Amount,
                  uploader.progressAmount, uploader.progressTotal);
      });
      uploader.on('end', function () {
          console.log("done uploading");
      });
  
  }
  
  function promiseWhile(condition, body) {
      var done = RSVP.defer();
  
      function loop() {
          // When the result of calling `condition` is no longer true, we are
          // done.
          if (!condition())
              return done.resolve();
          // Use `when`, in case `body` does not return a promise.
          // When it completes loop again otherwise, if it fails, reject the
          // done promise
          body().then(loop, done.reject);
      }
  
      // Start running the loop in the next tick so that this function is
      // completely async. It would be unexpected if `body` was called
      // synchronously the first time.
      RSVP.async(loop);
  
      // The promise
      return done.promise;
  }
  
  
  //readFiles('../../test_aws/', function(filename) {
  //  console.log(filename);
  //}, function(error) {
  //  console.log(error);
  //});
  
  // display contents of files in this script's directory
  dir.readFiles('/xampp/htdocs/test_aws',
          function (err, content, next) {
              if (err)
                  throw err;
              //console.log('content:', content);
              next();
          },
          function (err, files) {
              if (err)
                  throw err;
              //console.log('finished reading files:', files);
              var fileCount = files.length;
  
              /* 
               *  error for loop
               *  
               * @param {type} condition
               * @param {type} body
               * @returns {RSVP@call;defer.promise}
               */
              //for (var i = 0; i < fileCount; i++) {
              //    var fileName = files[i];
              //  console.log(fileName)
              //
              //uploader(fileName,nextYear);
              //}
  
  
  
              /*
               * Asynchronously decrementing a variable
               * The stylish route.
               */
              var i = fileCount;
              promiseWhile(function () {
                  return i > 0;
              }, function () {
                  return new RSVP.Promise(function (resolve, reject) {
                      setTimeout(function () {
                          i--;
                          uploader(files[i],nextYear);
                          console.log(files[i],i);
                          
                          resolve();
                      }, 500);
                  });
              }).then(function () {
                  console.log('done');
              });
  
  
  
  
          });