Zack Burt
Zack Burt

Reputation: 8455

All of a sudden, my Amazon S3 HTTP requests aren't working. Signature Does Not Match error. Help?

My code was working just fine a couple days ago. Then, all of a sudden, BAM: it stopped working. My PUTs stopped going through, with a SignatureDoesNotMatch error. Help?

require_once 'Crypt/HMAC.php';
require_once 'HTTP/Request.php';


function uploadFile($path_to_file, $store_file_as, $bucket, $debugmode = false) {

        $S3_URL = "http://s3.amazonaws.com/";
        $filePath = $path_to_file;
        $contentType = 'audio/mpeg';
        $keyId = 'THISISMYKEY, YES I DOUBLE CHECKED IT';
        $secretKey = 'THIS IS MYSECRET, YES I DOUBLED CHECKED IT';
        $key = $store_file_as;
        $resource = $bucket . "/" . $key;
        $acl = "public-read";
        $verb = "PUT";
    $httpDate = gmdate("D, d M Y H:i:s T");
    $stringToSign = "PUT\n\naudio/mpeg\n$httpDate\nx-amz-acl:$acl\n/$resource";
    $hasher =& new Crypt_HMAC($secretKey, "sha1");
    $str =  $hasher->hash($stringToSign);
    $raw = '';
    for ($i=0; $i < strlen($str); $i+=2) {
        $raw .= chr(hexdec(substr($str, $i, 2)));
    }
    $signature = base64_encode($raw);

    $req =& new HTTP_Request($S3_URL . $resource);
    $req->setMethod('PUT');
    $req->addHeader("content-type", $contentType);
    $req->addHeader("Date", $httpDate);
    $req->addHeader("x-amz-acl", $acl);
    $req->addHeader("Authorization", "AWS " . $keyId . ":" . $signature);
    $req->setBody(file_get_contents($filePath));  
    $req->sendRequest();
   echo $req->getResponseBody();
   }

Upvotes: 1

Views: 2377

Answers (1)

Alex Reynolds
Alex Reynolds

Reputation: 96927

Run your signature against the Amazon S3 JavaScript signature tester.

If the two signatures do not match, you know something is wrong with the keys or signing procedure.

If the two match, your keys and signing process are correct and the problem is somewhere else.

The JS tester is invaluable for troubleshooting signature generation problems.

Upvotes: 4

Related Questions