我们在S3上的自定义域上托管了大量视频/音频/媒体,并创建了一组功能,以便对URL进行签名并允许它们可流式传输和下载。问题是签名的URL当然不起作用。错误是:
我们计算的请求签名与您提供的签名不匹配。检查您的密钥和签名方法。
当然如果我们从这个页面返回的字节码并将其输入Amazon S3 Signature Tester并从那里获取字节码就可以了。即使从我们的函数签名的字符串以及签名测试器中的解码字节代码是相同的,它也永远不会有效。
通过一小段PHP代码调用它:
$headers = createS3parameters($expiry, $file_type);
$request = preg_replace("/^.*?:\/\/.*\//", "/", $bucketurl);
$signature = signRequest($request, $expiry, $s3secret, $headers, "GET", $file_type);
$signed_request = "$bucketurl?AWSAccessKeyId=$s3key&Expires=$expiry&$headers&Signature=$signature";
这是实际签名的功能。
function signRequest($request, $expiration, $s3secret, $headers = '', $type = 'GET', $content_type = 'default')
{
if ($expiration == 0 || $expiration == null)
{
$expiration = time() + 315576000; // 10 years (never)
}
if (strcmp($content_type, 'default') == 0)
{
$content_type = "";
}
// S3 tester spits out this format
/*$string = "$type\n".
"\n\n".
"$expiration\n".
"/mybucket$request?$headers";*/
$string = "$type\n".
"\n".
"$content_type\n".
"$expiration\n".
"$headers\n".
"$request";
// must be in UTF8 format
$string = utf8_encode(trim($string));
// encode to binary hash using sha1. require S3 bucket secret key
$hash = hash_hmac("sha1",$string, $s3secret,false);
// sha1 hash must be base64 encoded
$hash = base64_encode($hash);
// base64 encoded sha1 hash must be urlencoded
$signature = rawurlencode($hash);
return $signature;
}
然后创建一个URL,例如:
http://mybucket.s3.amazonaws.com/My_Boring_Video.wmv?AWSAccessKeyId=AKIAIEXAMPLE6GA3WYQ&Expires=1344160808&response-content-type=application/force-download&response-expires=1344160808&Signature=OTIxOTI0YjNjMTA1NjMyNmJjYTk0MGE2YWJkMmI5OWQ3MGM2ZGY0MQ%3D%3D
遗憾的是,这不起作用。这里有一个明显的问题吗?我一直盯着太长时间才能弄清楚?
答案 0 :(得分:2)
更新:规格是规格,但只有在符合实际情况时才有帮助。
亚马逊的S3规格称签名应如下形成:
Signature = URL-Encode( Base64( HMAC-SHA1( YourSecretAccessKeyID, UTF-8-Encoding-Of( StringToSign ) ) ) );
StringToSign = HTTP-VERB + "\n" +
Content-MD5 + "\n" +
Content-Type + "\n" +
Expires + "\n" +
CanonicalizedAmzHeaders +
CanonicalizedResource;
HOWEVER 所需的实际请求如下所示:
StringToSign = HTTP-VERB + "\n" +
"\n" +
"\n" +
Expires + "\n" +
Bucket + CanonicalizedResource + "?" + CanonicalizedAmzHeaders;
奇怪的是,在PHP中你似乎也无法做到这一点:
$string = "$type\n".
"\n".
"\n".
"$expiration\n".
"/$bucket$request?$headers";
它会更改签名并最终被拒绝,所以必须全部在一行上。我还没有检查这是我们特定版本的PHP中的错误还是仅仅是一般的PHP错误(或者可能是预期的功能??)。即使您使用的是虚拟URL,例如mybucket.s3.amazonaws.com或mybucket.mydomain.com,您也必须包含存储桶的名称。 documentation没有指定你能做什么或不能做什么,我假设由于我们使用的是基于S3的虚荣URL,它(S3)会选择域名并将其转换为桶
我最终将我的功能更改为以下内容:
function signRequest($bucket, $request, $expiration, $s3secret, $headers = '', $type = 'GET', $content_type = 'default')
{
if ($expiration == 0 || $expiration == null)
{
$expiration = time() + 315576000; // 10 years (never)
}
if (strcmp($content_type, 'default') == 0)
{
$content_type = "";
}
$headers = trim($headers, '&');
// This is the spec:
/*$string = "$type\n".
"\n".
"$content_type\n".
"$expiration\n".
"$headers\n".
"$bucket$request";*/
// but it will only work as this
$string = "$type\n\n\n$expiration\n/$bucket$request?$headers";
// this could be a single line of code but left otherwise for readability
// must be in UTF8 format
$string = utf8_encode(trim($string));
// encode to binary hash using sha1. require S3 bucket secret key
$hash = hash_hmac("sha1",$string, $s3secret,true);
// sha1 hash must be base64 encoded
$hash = base64_encode($hash);
// base64 encoded sha1 hash must be urlencoded
$signature = urlencode($hash);
return $signature;
}
希望其他人也觉得这很有用。
UPDATE(20180109):添加调用此函数的函数(也就是说让它变得简单易行)。
它有助于理解传递给signRequest()函数的内容。
private function genS3QueryString($bucketurl)
{
$file_type = 'application/force-download';
$expiry = '1831014000'; //Sun, Jan 09 2028 0700 UTC
$headers = '&response-content-type='.$file_type.'&response-expires='.$expiry;
$bucket = preg_replace("/^.*?:\/\/(.*)\.s3\.amazonaws\.com\/.*/", "$1", $bucketurl);
$request = preg_replace("/^.*?:\/\/.*\//", "/", $bucketurl);
$signature = $this->signRequest($bucket, $request, $expiry, S3_SECRET_KEY, $headers, 'GET', $file_type);
$signed_request = '?AWSAccessKeyId='.S3_KEY.'&Expires='.$expiry.$headers.'&Signature='.$signature;
return $signed_request;
}
但是请注意,第一个函数仅生成附加到AWS的GET请求所需的加密签名部分。根据文档(参见上面的链接),请求需要更多,正如上面的第二个函数所形成的那样。
如果您愿意,我认为到期时间可以是滚动到期时间,但未来应该充分说明资产在实际到期时间之前很久就会过时和更换。选择任意时间以使网站的当前版本足够长。
第二个功能只需要受保护资产的存储区网址。可以将所需的响应类型添加到呼叫中,或者只是更改,以便将资产(在这种情况下只是不可显示的)文档作为不同类型下载。
然后返回的 signed_request 必须附加到bucketurl上,以便从受保护的S3资产请求工作URI。