亚马逊S3在PHP签名网址 [英] Amazon S3 Signed URL in PHP
本文介绍了亚马逊S3在PHP签名网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是函数拉出旧WP插件的返回签署Amazon S3的网址,但我不能得到它的工作!当我访问它返回的标识的URL,我得到这样的:
我们计算没有您提供的签名和请求签名。检查你的密钥和签名方法。
函数s3Url($文本){
$ AWS_S3_KEY =钥匙;
$ AWS_S3_SECRET =秘密;
$ tag_pattern ='/(\ [。?S3存储\ =(*)\文\ =(*)\。?(*)\ [\ / S3 \]。?)/ I';
定义(AWS_S3_KEY,$ AWS_S3_KEY); //你的AWS S3键替换此
定义(AWS_S3_SECRET,$ AWS_S3_SECRET); //你的密钥代替。
$ =到期时间()+ get_option('expire_seconds');
如果(preg_match_all($ tag_pattern,$文字,$匹配)){
为($ M = 0; $ M<计数($比赛[0]); $ M +){
$桶= $匹配[2] [$ M]。
$ LINK_TEXT = $匹配[3] [$ M]。
$资源= $比赛[4] [$ M]。
$ string_to_sign =GET \ñ\ñ\ n $的到期\ N /str_replace函数(,$斗。s3.amazonaws.com。)/ $资源。
// $ string_to_sign =GET \ñ\ñ\ N {$到期} \ N / {$桶} / {$资源};
$签字= urlen code(base64_en code((hash_hmac(SHA1,utf8_en code($ string_to_sign),AWS_S3_SECRET,TRUE))));
$ authentication_params =AWSAccessKeyId =AWS_S3_KEY。
$ authentication_params =。与&过期= {$过期};
$ authentication_params =&放大器;签名= {$签名}。
$ tag_pattern_match =/(\ [。?S3存储\ =(*)\文\ = {$ LINK_TEXT} \] {$资源} \ [\ / S3 \])/我;
如果(strlen的($ LINK_TEXT)== 0)
{
$链接=HTTP:// {$桶} / {$资源} {$ authentication_params}?;
}
其他
{
$链接=&其中; A HREF ='的http:// {$桶} / {$资源} {$ authentication_params}?'> {$ LINK_TEXT}&所述; / A>中;
}
$文字= preg_replace($ tag_pattern_match,$链接,$文字);
}
}
返回$文字;
}
解决方案
在亚马逊AWS PHP SDK提供的示例: SDK-最新\ SDK-1.3.5 \ SDK-1.3.5 \ _samples \ CLI-s3_get_urls_for_uploads.php
以下code工作得很好
/ *执行我们的批处理请求的队列。这可能需要几秒钟到
根据文件的大小和速度上载几分钟
速度。 * /
$ file_upload_response = $ S3->批次() - >发();
/ *由于一批请求将返回多个响应,让我们
确保他们都回来了成功使用`areOK()`(单数
回复使用'isOK()`)。 * /
如果($ file_upload_response-> areOK())
{
//循环遍历各个文件名
的foreach($ individual_filenames为$文件名)
{
/ *显示每个我们上传的文件的URL。由于上传默认
私人(你可以选择上传时覆盖此设置),我们将
$ P $在接下来的5分钟对验证文件的URL。 * /
回声$ S3-> get_object_url($桶,$文件名,5分钟)。 PHP_EOL。 PHP_EOL;
}
}
This is the function pulled out of an old WP plugin for returning a signed Amazon S3 URL, but I can't get it to work! When I visit the signed URL it returns, I get this:
The request signature we calculated does not match the signature you provided. Check your key and signing method.
function s3Url($text) {
$AWS_S3_KEY = 'KEY';
$AWS_S3_SECRET = 'SECRET';
$tag_pattern = '/(\[S3 bucket\=(.*?)\ text\=(.*?)\](.*?)\[\/S3\])/i';
define("AWS_S3_KEY", $AWS_S3_KEY); // replace this with your AWS S3 key
define("AWS_S3_SECRET", $AWS_S3_SECRET); // replace this with your secret key.
$expires = time()+get_option('expire_seconds');
if (preg_match_all ($tag_pattern, $text, $matches)) {
for ($m=0; $m<count($matches[0]); $m++) {
$bucket = $matches[2][$m];
$link_text = $matches[3][$m];
$resource = $matches[4][$m];
$string_to_sign = "GET\n\n\n$expires\n/".str_replace(".s3.amazonaws.com","",$bucket)."/$resource";
//$string_to_sign = "GET\n\n\n{$expires}\n/{$bucket}/{$resource}";
$signature = urlencode(base64_encode((hash_hmac("sha1", utf8_encode($string_to_sign), AWS_S3_SECRET, TRUE))));
$authentication_params = "AWSAccessKeyId=".AWS_S3_KEY;
$authentication_params.= "&Expires={$expires}";
$authentication_params.= "&Signature={$signature}";
$tag_pattern_match = "/(\[S3 bucket\=(.*?)\ text\={$link_text}\]{$resource}\[\/S3\])/i";
if(strlen($link_text) == 0)
{
$link = "http://{$bucket}/{$resource}?{$authentication_params}";
}
else
{
$link = "<a href='http://{$bucket}/{$resource}?{$authentication_params}'>{$link_text}</a>";
}
$text = preg_replace($tag_pattern_match,$link,$text);
}
}
return $text;
}
解决方案
The example provided in the Amazon AWS PHP SDK: sdk-latest\sdk-1.3.5\sdk-1.3.5\_samples\cli-s3_get_urls_for_uploads.php
the following code works quite well:
/* Execute our queue of batched requests. This may take a few seconds to a
few minutes depending on the size of the files and how fast your upload
speeds are. */
$file_upload_response = $s3->batch()->send();
/* Since a batch of requests will return multiple responses, let's
make sure they ALL came back successfully using `areOK()` (singular
responses use `isOK()`). */
if ($file_upload_response->areOK())
{
// Loop through the individual filenames
foreach ($individual_filenames as $filename)
{
/* Display a URL for each of the files we uploaded. Since uploads default to
private (you can choose to override this setting when uploading), we'll
pre-authenticate the file URL for the next 5 minutes. */
echo $s3->get_object_url($bucket, $filename, '5 minutes') . PHP_EOL . PHP_EOL;
}
}
这篇关于亚马逊S3在PHP签名网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文