2009-09-02 25 views
1

我的代碼幾天前工作得很好。然後,突然間,BAM:它停止了工作。我的PUT停止了,發生SignatureDoesNotMatch錯誤。幫幫我?突然間,我的Amazon S3 HTTP請求無法正常工作。簽名不匹配錯誤。幫幫我?

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(); 
    } 

回答

4

Amazon S3 JavaScript signature tester運行您的簽名。

如果兩個簽名不匹配,則您知道密鑰或簽名過程有問題。

如果兩者匹配,您的密鑰和簽名過程是正確的,並且問題在其他地方。

JS測試儀對於解決簽名生成問題是非常有用的。

+0

謝謝,這是一個驚人的資源。 – 2009-09-02 13:38:17

相關問題