AWS S3签名不匹配(从html表单上传)

时间:2018-04-06 02:15:34

标签: php amazon-web-services amazon-s3

我正在尝试使用this示例上传文件,这是我的第一个AWS项目。这是我到目前为止的PHP代码:

<?php
require 'app/startAWS.php';
$conf = require('app/config.php');

$expiration = "2018-04-07T00:00:00Z";
$currentV2 = "20180406T000000Z";
$currentDate = "20180406";

$success_action_redirect = 'http://localhost/';
$policydoc = 
    <<<EOT
    { "expiration": "$expiration",
      "conditions": [
        {"bucket": "bucketx"},
        ["starts-with", "\$key", "testDir/"],
        {"acl": "private"},
        {"success_action_redirect": "http://test.com"},
        ["starts-with", "\$Content-Type", "image"],
        {"x-amz-meta-uuid": "14365123651274"},
        {"x-amz-credential": 
            "{$conf['s3']['key']}/{$currentDate}/
            {$conf['s3']['region']}/s3/aws4_request"},
        {"x-amz-algorithm": "AWS4-HMAC-SHA256"},
        {"x-amz-date": "$currentV2" }
      ]
    }
    EOT;

$policydoc = base64_encode($policydoc);
$signature = base64_encode(hash_hmac('sha1', $policydoc, $conf['s3']['secret'], true));
?>
编码之前

$policydoc

{ "expiration": "2018-04-07T00:00:00Z",
  "conditions": [
    {"bucket": "bucketx"},
    ["starts-with", "$key", "testDir/"],
    {"acl": "private"},
    {"success_action_redirect": "http://test.com"},
    ["starts-with", "$Content-Type", "image"],
    {"x-amz-meta-uuid": "14365123651274"},
    {"x-amz-credential": "AKIAJDFNFVWUVFNMXXXX/20180406/eu-central-1/s3/aws4_request"},
    {"x-amz-algorithm": "AWS4-HMAC-SHA256"},
    {"x-amz-date": "20180406T000000Z" }
  ]
}

HTML输出

<form action="http://bucketx.s3.amazonaws.com/" method="post" enctype="multipart/form-data">
Key to upload: 
  <input type="input" name="key" value="testDir/${filename}"><br>
  <input type="hidden" name="acl" value="private">
  <input type="hidden" name="success_action_redirect" value="http://test.com">
Content-Type: 
  <input type="input" name="Content-Type" value="image/jpeg"><br>
  <input type="hidden" name="x-amz-meta-uuid" value="14365123651274"> 
  <!--<input type="hidden" name="x-amz-server-side-encryption" value="" />-->
  <input type="text" name="X-Amz-Credential" value="AKIAJDFNFVWUVFNMXXXX/20180406/eu-central-1/s3/aws4_request">
  <input type="text" name="X-Amz-Algorithm" value="AWS4-HMAC-SHA256">
  <input type="text" name="X-Amz-Date" value="20180406T000000Z">
Tags for File: 
  <!--<input type="input"  name="x-amz-meta-tag" value="" /><br />-->
  <input type="hidden" name="Policy" value="eyAiZXhwaXJhdGlvbiI6ICIyMDE4LTA0LTA3VDAwOjAwOjAwWiIsDQogICJjb25kaXRpb25zIjogWw0KICAgIHsiYnVja2V0IjogIm9ycnlvbiJ9LA0KICAgIFsic3RhcnRzLXdpdGgiLCAiJGtleSIsICJ0ZXN0RGlyLyJdLA0KICAgIHsiYWNsIjogInByaXZhdGUifSwNCiAgICB7InN1Y2Nlc3NfYWN0aW9uX3JlZGlyZWN0IjogImh0dHA6Ly9vcnJ5b24uY29tIn0sDQogICAgWyJzdGFydHMtd2l0aCIsICIkQ29udGVudC1UeXBlIiwgImltYWdlIl0sDQogICAgeyJ4LWFtei1tZXRhLXV1aWQiOiAiMTQzNjUxMjM2NTEyNzQifSwNCiAgICB7IngtYW16LWNyZWRlbnRpYWwiOiAiQUtJQUpERk5GVldVVkZOTUZBT1EvMjAxODA0MDYvZXUtY2VudHJhbC0xL3MzL2F3czRfcmVxdWVzdCJ9LA0KICAgIHsieC1hbXotYWxnb3JpdGhtIjogIkFXUzQtSE1BQy1TSEEyNTYifSwNCiAgICB7IngtYW16LWRhdGUiOiAiMjAxODA0MDZUMDAwMDAwWiIgfQ0KICBdDQp9">
  <input type="hidden" name="X-Amz-Signature" value="oXkMfvo+19wTJ9bA43cFWIte1C0=">
File: 
  <input type="file" name="file"> <br>
  <input type="submit" name="submit" value="Upload to Amazon S3">
</form>

我不在S3中使用加密,所以我把它留了出来。

我收到错误The request signature we calculated does not match the signature you provided. Check your key and signing method.,但我的密钥在我的签名方法出错之前就已经工作了。在我看来,我的签名很短(oXkMfvo+19wTJ9bA43cFWIte1C0=),但我不确定这是不是问题。那我到底做错了什么?

0 个答案:

没有答案