2016-07-20 45 views
0

我試圖發送一個PUT請求到亞馬遜兼容的存儲,我不斷收到以下錯誤:簽名生成在亞馬遜S3使用Javascript

"<Error><Code>SignatureDoesNotMatch</Code><Message>The request signature we calculated does not match the signature you provided. Check your Secret Access Key and signing method. For more information, see REST Authentication and SOAP Authentication for details.</Message><RequestId>0af9f985:155f11613d1:1732:13</RequestId></Error>" 

有人能告訴我如何生成正確的簽名在Javascript中使用祕密訪問密鑰和其他參數。我知道我使用的是正確的憑證,因爲Amazon S3支持的所有其他操作都在使用Amazon S3 SDK工作。我通過這個鏈接: http://docs.aws.amazon.com/general/latest/gr/signature-v4-examples.html,但我無法找到該方法所期望的每個參數。

添加我寫的代碼。這是幾乎一樣的,你在使用Javascript SDK,只有方法canonicalizedResource看,更簡單:

private stringToSign(request: any) { 
     var parts = []; 
     parts.push(request.headers['method']); 
     parts.push(request.headers['Content-MD5'] || ''); 
     parts.push(request.headers['Content-Type'] || ''); 
     parts.push(request.headers['presigned-expires'] || ''); 

     var headers = this.canonicalizedAmzHeaders(request); 
     if (headers) parts.push(headers); 
     parts.push(this.canonicalizedResource(request)); 
     return parts.join('\n');; 
    } 

    private canonicalizedAmzHeaders(request: any) { 
     var amzHeaders = []; 
     AWS.util.each(request.headers, function(name) { 
      if (name.match(/^x-amz-/i)) 
       amzHeaders.push(name); 
     }); 
     amzHeaders.sort(function(a, b) { 
      return a.toLowerCase() < b.toLowerCase() ? -1 : 1; 
     }); 
     var parts = []; 
     AWS.util.each(amzHeaders, function(name) { 
      parts.push(amzHeaders[name].toLowerCase() + ':' + request.headers[amzHeaders[name]]); 
     }); 
     return parts.join('\n'); 
    } 

    private canonicalizedResource(request) { 

     return request.path || '/'; 
    } 

我打電話就像方法:

var signature = this.sign(AWS.config.credentials.secretAccessKey, this.stringToSign(request)); 
+0

你也可以粘貼你用來生成簽名的代碼嗎? – error2007s

+0

請在問題描述中找到代碼。 -謝謝。 – UnderWood

+0

「亞馬遜兼容存儲?」其實不是S3?簽名版本4算法具有[測試套件](http://docs.aws.amazon.com/general/latest/gr/signature-v4-test-suite.html),它不僅向您提供樣本憑證,請求,結果以及中間值來幫助您集中解決問題,以幫助您發現代碼出了什麼問題。 –

回答

0

你可以確保你的系統時間已是最新?我的系統時間不同步時遇到問題。 AWS拒絕這些API調用。