--- a/amazons3.module	Sat Apr 21 14:24:14 2012
+++ b/amazons3.module	Mon Jul 08 23:24:03 2013
@@ -33,7 +33,13 @@
     'page arguments' => array('amazons3_admin'),
     'access arguments' => array('access administration pages'),
   );
-
+  $items['system/amazon/files'] = array(
+    'title' => 'Download amazon file',
+    'description' => 'Download files from amazon used by commerce file module',
+    'page callback' => 'amazons3_commerce_file_download',
+    'page arguments' => array(3),
+    'access callback' => TRUE,
+  );
   return $items;
 }
 
@@ -183,3 +189,122 @@
   drupal_set_message(t('Cache cleared.'));
 }
 
+
+/**
+ * Implements hook_url_outbound_alter.
+ */
+function amazons3_url_outbound_alter(&$path, &$options, $original_path) {
+  if ($options['external'] == 1 && module_exists('amazons3')) {
+    if (variable_get('amazons3_cname', 0)) {
+      $bucket = variable_get('amazons3_bucket', '');
+      $amazon_domain = variable_get('amazons3_domain', '');
+      if (strlen($amazon_domain) > 0) {
+        $amazon_url = 'http://' . $amazon_domain;
+      }
+      else {
+        $amazon_url = 'http://' . $bucket;
+      }
+    }
+    else {
+      $amazon_url = 'http://' . $bucket . '.s3.amazonaws.com';
+    }
+    if (strstr($path, $amazon_url)) {
+      $path_length = strlen($path) - strlen($amazon_url) - 1;
+      $relative_path = substr($path, -$path_length);
+      $path = base_path() . 'system/amazon/files/' . base64_encode($relative_path);
+    }
+  }
+}
+
+/**
+ * Function to call before user goto external site for necessary download calculations.
+ */
+function amazons3_commerce_file_download($uri) {
+  global $user;
+  // Setting uri fro amazon store file
+  $uri = 's3://' . base64_decode($uri);
+
+  $account = clone($user);
+  $file = NULL;
+
+  // Get the file record based on the URI. If not in the database just return.
+  // @see file_file_download()
+  $files = file_load_multiple(array(), array('uri' => $uri));
+  //print_r($files); exit;
+  if (empty($files)) {
+    return;
+  }
+  if (count($files)) {
+    foreach ($files as $item) {
+      // Since some database servers sometimes use a case-insensitive comparison
+      // by default, double check that the filename is an exact match.
+      if ($item->uri === $uri) {
+        $file = $item;
+        break;
+      }
+    }
+  }
+
+  // PASS if the file does not exist
+  if (!isset($file)) {
+    return;
+  }
+
+  // PASS if the file is not permanent (ie temporary)
+  if ($file->status != FILE_STATUS_PERMANENT) {
+    return;
+  }
+
+  // determine if file is associated with a commerce_file field
+  $references = _commerce_file_get_file_references($file, NULL, FIELD_LOAD_CURRENT, COMMERCE_FILE_FIELD_TYPE);
+
+  // PASS if not a file in any commerce_file field
+  if (empty($references)) {
+    return;
+  }
+
+  // ALLOW if user has 'view' access with no license (ie. admin)
+  // - Note: This stops any license logging for admin users
+  if (commerce_file_license_admin_access('view', $account)) {
+    //return commerce_file_get_content_headers($file);
+    header('Location: ' . file_create_url($uri));
+    exit;
+  }
+
+  // get user's licenses for this file
+  $licenses = commerce_file_license_load_by_property(array($file->fid), array(), $account);
+
+  // check license based access
+  if (!empty($licenses)) {
+    // find a valid license that can be downloaded by the user
+    ksort($licenses);
+    foreach ($licenses as $license_id => $license) {
+      // trigger download hook and check download was allowed by the hook actions
+      if ($license->can_download($account)) {
+        // return on first license allowed
+        // set time limit for large files
+        drupal_set_time_limit(0);
+
+        // store current licenses in this request for use in hook_exit()
+        //_commerce_file_license_set_request_license($license);
+        commerce_file_license_log_save(array('license_id' => $license->license_id));
+        // return additional file headers and PASS to file module and others to handle
+        //return commerce_file_get_content_headers($file);
+        header('Location: ' . file_create_url($uri));
+        exit;
+      }
+    }
+  }
+
+  // if do not have any valid licenses ...
+  // let other modules ALLOW unlicensed access to the protected file
+  $grants = _commerce_file_download_unlicensed_access($file, $account, $licenses);
+  if (in_array(TRUE, $grants)) {
+    // If TRUE is returned, access is granted
+    //return commerce_file_get_content_headers($file);
+    header('Location: ' . file_create_url($uri));
+    exit;
+  }
+  // DENY ALL
+  return drupal_access_denied();
+}
