Scalable
Scalable

Reputation: 1681

AWS S3 QueryString Authorization

Background

I am trying to create a signed url to fetch a resource from AWS S3. There are several examples in aws s3 documentation , and as a first step I am trying to replicate the last example on that page.

My code is :

$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" ;

The above code outputs

MzY5ODAyOGU3MGYzYWNjZjk2MTczYTA0MzU3OWE5MzQzNTJjNGE3Zg==

According to the example, the result should be

NpgCjnDzrM%2BWFzoENXmpNDUsSn8%3D&

I do understand that the result needs to be url encoded , but I think I am still far off. Can I get some help please ?

Upvotes: 3

Views: 919

Answers (1)

Scalable
Scalable

Reputation: 1681

I found the solution

$encode = hash_hmac('sha1', utf8_encode($string_to_sign), $secret_key);

is replaced by

$encode = hash_hmac('sha1', utf8_encode($string_to_sign), $secret_key, true );

Final script to simulate the example is :

$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"  ;

Upvotes: 6

Related Questions