我正在尝试创建一个签名网址以从AWS S3获取资源。有several examples in aws s3 documentation,作为第一步,我尝试复制该页面上的最后一个示例。
$access_key = "AKIAIOSFODNN7EXAMPLE";
$secret_key = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY";
$string_to_sign = "GET\n\n\n1175139620\n/johnsmith/photos/puppy.jpg";
$encode = hash_hmac('sha1', utf8_encode($string_to_sign) , $secret_key) ;
echo base64_encode($encode). "\r\n" ;
以上代码输出
MzY5ODAyOGU3MGYzYWNjZjk2MTczYTA0MzU3OWE5MzQzNTJjNGE3Zg==
根据示例,结果应为
NpgCjnDzrM%2BWFzoENXmpNDUsSn8%3D&
我确实理解结果需要进行网址编码,但我认为我还有很长的路要走。我可以帮忙吗?
答案 0 :(得分:6)
$encode = hash_hmac('sha1', utf8_encode($string_to_sign), $secret_key);
替换为
$encode = hash_hmac('sha1', utf8_encode($string_to_sign), $secret_key, true );
模拟示例的最终脚本是:
$access_key = "AKIAIOSFODNN7EXAMPLE";
$secret_key = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY";
$string_to_sign = "GET\n\n\n1175139620\n/johnsmith/photos/puppy.jpg";
$encode = hash_hmac('sha1', utf8_encode($string_to_sign), $secret_key , true ) ;
echo urlencode(base64_encode($encode)) . "\r\n" ;