我正在尝试使用教程http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-post-example.html之后的HTTP post方法将图像上传到s3存储桶。但是我每次都会收到以下错误
Error
<Code>SignatureDoesNotMatch</Code>
<Message>
The request signature we calculated does not match the signature you
provided. Check your key and signing method.
</Message>
要发出请求,我有以下表格:
<form action="http://mynewdulibucket.s3.amazonaws.com/" method="post" enctype="multipart/form-data">
Key to upload:
<input type="input" name="key" value="user/user1/${filename}" /><br />
<input type="hidden" name="acl" value="public-read" />
<input type="hidden" name="success_action_redirect" value="http://mynewdulibucket.s3.amazonaws.com/successful_upload.html" />
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="AES256" />
<input type="text" name="X-Amz-Credential" value="XXXXXXXXXXXXX/20151229/us-east-1/s3/aws4_request" />
<input type="text" name="X-Amz-Algorithm" value="AWS4-HMAC-SHA256" />
<input type="text" name="X-Amz-Date" value="20151229T000000Z" />
Tags for File:
<input type="input" name="x-amz-meta-tag" value="" /> <br />
<input type="hidden" name="Policy" value="XXXXXXXXXXXXXXX"/>
<input type="hidden" name="X-Amz-Signature" value="XXXXXXXXXXXXX" />
File:
<input type="file" name="file" /> <br />
<!-- The elements after this will be ignored -->
<input type="submit" name="submit" value="Upload to Amazon S3" />
政策如下:
{ "expiration": "2018-12-30T12:00:00.000Z",
"conditions": [
{"bucket": "mynewdulibucket"},
["starts-with", "$key", "user/user1/MyPhoto.jpg"],
{"acl": "public-read"},
{"success_action_redirect": "http://mynewdulibucket.s3.amazonaws.com/successful_upload.html"},
["starts-with", "$Content-Type", "image/"],
{"x-amz-meta-uuid": "14365123651274"},
{"x-amz-server-side-encryption": "AES256"},
["starts-with", "$x-amz-meta-tag", ""],
{"x-amz-credential": "XXXXXXXXX/20151229/us-east-1/s3/aws4_request"},
{"x-amz-algorithm": "AWS4-HMAC-SHA256"},
{"x-amz-date": "20151229T000000Z" }
]
}
生成策略和签名的代码然后粘贴到相关的表单字段中:
public class Main {
public static void main(String[] args) throws Exception
{
String policy_document = "{\"expiration\":\"2018-12-30T12:00:00.000Z\",\"conditions\":[{\"bucket\":\"mynewdulibucket\"},[\"starts-with\",\"$key\",\"user/user1/MyPhoto.jpg\"],{\"acl\":\"public-read\"},{\"success_action_redirect\":\"http://mynewdulibucket.s3.amazonaws.com/successful_upload.html\"},[\"starts-with\",\"$Content-Type\",\"image/\"],{\"x-amz-meta-uuid\":\"14365123651274\"},{\"x-amz-server-side-encryption\":\"AES256\"},[\"starts-with\",\"$x-amz-meta-tag\",\"\"],{\"x-amz-credential\":\"AKIAJQHQNWQ7FCTGNKQQ/20151229/us-east-1/s3/aws4_request\"},{\"x-amz-algorithm\":\"AWS4-HMAC-SHA256\"},{\"x-amz-date\":\"20151229T000000Z\"}]}";
String encodedPolicy = new String(Base64.getEncoder().encode(policy_document.getBytes("UTF-8"))).replaceAll("\n", "").replaceAll("\r", "");
String secretKey = "XXXXXXXXXXXXXXXX";
String signature = getSigning(secretKey, "20151229T000000Z", "us-east-1", "s3",encodedPolicy);
//the following values get pasted into the form fields Policy and X-Amz-Signature respectively (see above XXXXX)
System.out.println("base64 " + encodedPolicy);
System.out.println("signature " + signature);
}
static byte[] HmacSHA256(String data, byte[] key) throws Exception
{
String algorithm="HmacSHA256";
Mac mac = Mac.getInstance(algorithm);
mac.init(new SecretKeySpec(key, algorithm));
return mac.doFinal(data.getBytes("UTF8"));
}
static String getSigning(String key, String dateStamp, String regionName, String serviceName,String base64signature) throws Exception {
byte[] kSecret = ("AWS4" + key).getBytes("UTF8");
byte[] kDate = HmacSHA256(dateStamp, kSecret);
byte[] kRegion = HmacSHA256(regionName, kDate);
byte[] kService = HmacSHA256(serviceName, kRegion);
//
byte[] kSigning = HmacSHA256("aws4_request", kService);
byte[] signature = HmacSHA256(base64signature, kSigning);
return new String(Base64.getEncoder().encode(signature));
}
答案 0 :(得分:-1)
如果未正确验证,则会出现此误导性错误。
验证: