这篇文章 https://css-tricks.com/sn-ps/php/generate-expiring-amazon-s3-link/ 可以帮助你。不要忘记检查 cmets。
*** 编辑
生成即将过期的 Amazon S3 链接
您不必公开 Amazon S3 上的文件(默认情况下不公开)。但是您可以生成特殊密钥以允许访问私有文件。这些密钥通过 URL 传递,并且可以设置为过期。
<?php
如果(!function_exists('el_crypto_hmacSHA1')){
函数 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) 。包('H *',sha1(
($key ^ $ipad) 。 $数据
))
));
返回 base64_encode($hmac);
}
}
if(!function_exists('el_s3_getTemporaryLink')){
函数 el_s3_getTemporaryLink($accessKey, $secretKey, $bucket, $path, $expires = 5) {
//计算过期时间
$expires = time() + intval(floatval($expires) * 60);
// 修复路径;编码和消毒
$path = str_replace('%2F', '/', rawurlencode($path = ltrim($path, '/')));
// 签名路径以bucket开头
$signpath = '/'。 $bucket .'/'。 $路径;
// 要签名的 S3 友好字符串
$signsz = implode("\n", $pieces = array('GET', null, null, $expires, $signpath));
// 计算哈希
$signature = el_crypto_hmacSHA1($secretKey, $signsz);
// 粘贴 URL ...
$url = sprintf('http://%s.s3.amazonaws.com/%s', $bucket, $path);
// ... 到查询字符串 ...
$qs = http_build_query($pieces = array(
'AWSAccessKeyId' => $accessKey,
'过期' => $过期,
'签名' => $签名,
));
// ... 并返回 URL!
返回 $url.'?'.$qs;
}
}
用法