s3上的文件网址与其他地方托管的网站上的文件相匹配

时间:2018-01-04 20:29:17

标签: url amazon-s3 drupal

首先,如果这是重复的话我会道歉,因为我已经到处寻找,答案是针对略有不同的情况,或者我只是无法让它们发挥作用。

我的情景: 在不支持大于250mb的文件的平台上托管Drupal站点。客户希望在网站上托管500mb的zip文件,并且推理是为了使文件的URL与站点上的任何其他文件相同。他们希望能够轻松删除文件,并在将来用新的大文件替换它。

更新:

  1. 我成功屏蔽了一个带有CNAME的子域名的s3文件网址,但这无法解决这个问题,即网址略有不同,需要它自己的ssl证书。

  2. 我使用s3fs模块将站点的默认文件位置设置为s3存储桶。现在在设置文件管理器模块elfinder时,我无法让它知道管理文件的新位置。 Elfinder假设它们位于本地默认文件位置。一旦我解决了这个问题,我就可以解决这个问题。

1 个答案:

答案 0 :(得分:0)

这不是一个完整的答案,只是我无法在评论中加入的一些代码。

此代码生成临时链接以下载存储在S3存储桶中的私有文件。

像这样使用:

$url = el_s3_getTemporaryLink('myaccesskey','mysecretkey','mybucket','linux.png', 1);// this link is alive for one minute.

将输出类似于:https://mybucket.s3.amazonaws.com/?sometoken

的内容

您可以将其放在以下链接中: l('Download now', $url, ['external'=>true]);

<?php 

if(!function_exists('el_crypto_hmacSHA1')){
  /**
  * Calculate the HMAC SHA1 hash of a string.
  *
  * @param string $key The key to hash against
  * @param string $data The data to hash
  * @param int $blocksize Optional blocksize
  * @return string HMAC SHA1
  */
  function el_crypto_hmacSHA1($key, $data, $blocksize = 64) {
      if (strlen($key) > $blocksize) $key = pack('H*', sha1($key));
      $key = str_pad($key, $blocksize, chr(0x00));
      $ipad = str_repeat(chr(0x36), $blocksize);
      $opad = str_repeat(chr(0x5c), $blocksize);
      $hmac = pack( 'H*', sha1(
      ($key ^ $opad) . pack( 'H*', sha1(
        ($key ^ $ipad) . $data
      ))
    ));
      return base64_encode($hmac);
  }
}

if(!function_exists('el_s3_getTemporaryLink')){
  /**
  * Create temporary URLs to your protected Amazon S3 files.
  *
  * @param string $accessKey Your Amazon S3 access key
  * @param string $secretKey Your Amazon S3 secret key
  * @param string $bucket The bucket (bucket.s3.amazonaws.com)
  * @param string $path The target file path
  * @param int $expires In minutes
  * @return string Temporary Amazon S3 URL
  * @see http://awsdocs.s3.amazonaws.com/S3/20060301/s3-dg-20060301.pdf
  */

  function el_s3_getTemporaryLink($accessKey, $secretKey, $bucket, $path, $expires = 5) {
    // Calculate expiry time
    $expires = time() + intval(floatval($expires) * 60);
    // Fix the path; encode and sanitize
    $path = str_replace('%2F', '/', rawurlencode($path = ltrim($path, '/')));
    // Path for signature starts with the bucket
    $signpath = '/'. $bucket .'/'. $path;
    // S3 friendly string to sign
    $signsz = implode("\n", $pieces = array('GET', null, null, $expires, $signpath));
    // Calculate the hash
    $signature = el_crypto_hmacSHA1($secretKey, $signsz);
    // Glue the URL ...
    $url = sprintf('http://%s.s3.amazonaws.com/%s', $bucket, $path);
    // ... to the query string ...
    $qs = http_build_query($pieces = array(
      'AWSAccessKeyId' => $accessKey,
      'Expires' => $expires,
      'Signature' => $signature,
    ));
    // ... and return the URL!
    return $url.'?'.$qs;
  }
}