不确定我的代码有什么问题.. php和html在实际页面中是分开的。
我从AWS回来的错误是:
我们计算的请求签名与您提供的签名不匹配。检查您的密钥和签名方法。
require '/aws/aws-autoloader.php';
use Aws\Common\Aws;
use Aws\S3\S3Client;
$aws = Aws::factory(array(
'key' => "XXX", 'secret' => "ZZZ"
));
$s3 = $aws->get("s3");
$policy = base64_encode('{
"expiration": "2016-08-06T12:00:00.000Z",
"conditions": [
{"bucket": "richqtest" },
["starts-with", "$key", "user/rdqrdq/${filename}"],
{"acl": "public-read" },
{"redirect": "http://cnn.com" },
["starts-with", "$Content-Type", "image/"],
{"x-amz-meta-uuid": "14365123651274"},
["starts-with", "$x-amz-meta-tag", ""],
{"x-amz-credential": "XXX/20160806/us-west-2/s3/aws4_request"},
{"x-amz-algorithm": "AWS4-HMAC-SHA256"},
{"x-amz-date": "20160806T000000Z" }
]
}');
$secret = "ZZZ";
$signature = base64_encode(hash_hmac('sha256', $policy, $secret, true));
<form action="http://richqtest.s3.amazonaws.com/" method="post" enctype="multipart/form-data">
Key to upload:
<input type="input" name="key" value="user/rdqrdq/${filename}" /><br />
<input type="hidden" name="acl" value="public-read" />
<input type="hidden" name="success_action_redirect" value="http://cnn.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="text" name="X-Amz-Credential" value="XXX/20160806/us-west-2/s3/aws4_request" />
<input type="text" name="X-Amz-Algorithm" value="AWS4-HMAC-SHA256" />
<input type="text" name="X-Amz-Date" value="20160806T000000Z" />
Tags for File:
<input type="input" name="x-amz-meta-tag" value="" /><br />
<input type="hidden" name="Policy" value="<?php echo $policy ?>" />
<input type="hidden" name="X-Amz-Signature" value="<?php echo $signature ?>" />
File:
<input type="file" name="file" /> <br />
<!-- The elements after this will be ignored -->
<input type="submit" name="submit" value="Upload to Amazon S3" />