2012-05-27 118 views
5

我花了三天的時間試圖設置一個簡單的發佈表單到亞馬遜s3。 每次我得到這個錯誤:將表單數據發佈到亞馬遜S3存儲桶

SignatureDoesNotMatchThe request signature we calculated does not match the signature you provided. Check your key and signing method.

我沒有看到這個問題。 :-(

<?php 
     $form = array(
      'key'      => 'queue/1_1_1234567890.wmv', 
      'AWSAccessKeyId'   => 'mypublickeyishere', 
      'acl'      => 'public-read', 
      'success_action_redirect' => 'http://someurl.com', 
     ); 

     $form['policy'] = '{ 
      "expiration": "2015-12-01T12:00:00.000Z", 
       "conditions": [ 
        { 
         "acl": "'.$form['acl'].'" 
        }, 
        { 
         "success_action_redirect": "'.$form['success_action_redirect'].'" 
        }, 
        { 
         "bucket": "thenameofmybucket" 
        }, 
        [ 
         "starts-with", 
         "$key", 
         "queue/" 
        ] 
       ] 
      }'; 

    $form['policy_encoded'] = base64_encode($form['policy']); 
    $form['signature'] = base64_encode(hash_hmac('sha1', base64_encode(utf8_encode($form['policy'])), 'F90mc5kpjuNMPg8XG7iV6bxOzacYhktcw+RVGzpZ')); 

?> 


<form action="https://thenameofmybucket.s3.amazonaws.com/" method="post" enctype="multipart/form-data"> 
     <input type="hidden" name="key" value="<?php echo $form['key'] ?>"> 
     <input type="hidden" name="AWSAccessKeyId" value="<?php echo $form['AWSAccessKeyId'] ?>"> 
     <input type="hidden" name="acl" value="<?php echo $form['acl'] ?>"> 
     <input type="hidden" name="success_action_redirect" value="<?php echo $form['success_action_redirect'] ?>"> 
     <input type="hidden" name="policy" value="<?php echo $form['policy_encoded'] ?>"> 
     <input type="hidden" name="signature" value="<?php echo $form['signature'] ?>"> 

     File to upload to S3: 
     <input name="file" type="file"> 
     <br> 
     <input type="submit" value="Upload File to S3"> 
</form> 

我取代水桶名稱以及上面的私鑰和公鑰

我跟着指示,精心籤政策: http://docs.amazonwebservices.com/AmazonS3/2006-03-01/dev/HTTPPOSTForms.html#HTTPPOSTConstructPolicy

我缺少什麼?爲什麼代碼不工作?

回答

-1

好吧,我終於設法使用這個示例代碼庫完成這個工作: http://aws.amazon.com/code/Amazon-S3/1618

+0

順便說一句,瀏覽器/操作系統崩潰了,因爲我發送的表單字段不在策略中。將x-ignore-添加到字段後,它就可以工作。不知道爲什麼會導致崩潰。 – reggie

+3

您應該接受實際包含遇到問題解決方案的答案。 –

-1

已經回答了你自己的問題,我意識到了,但是我想知道這個教程(http://aws.amazon.com/articles/1434),這表明你必須在執行base 64編碼之前從json中去掉返回字符是問題的根源嗎?

16

沒有必要爲那個庫,你只是缺少一個參數。問題是你沒有設置hash_hmac函數來輸出二進制數據。要做到這一點,第四個參數設置爲true像這樣:

$signature = base64_encode(hash_hmac('sha1', $policy_b64, $secret, true)); 

如果不設置此,它不會是編碼簽名AWS期望它的方式。

+0

我有完全相同的問題,這正是我需要的。謝謝。 (這應該是被接受的答案。) –

+0

同上。是的,這應該是公認的答案,因爲它清楚地解決了原來的問題。好吧!無論如何,請有一個upvote。 –