我的 s3 邮寄表格有问题。我虽然读了http://aws.amazon.com/articles/1434/
我遵循了整个教程:
import sun.misc.BASE64Encoder;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
public class Storage {
public static String awsAccessKey = "xxxxxxxxxxxxxxx";
public static String awsSecretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
static String policy_document = "{\"expiration\": \"2013-01-01T00:00:00Z\","
+ "\"conditions\": ["
+ "{\"bucket\": \"kanta-assets\"},"
+ "[\"starts-with\", \"$key\", \"\"],"
+ "{\"acl\": \"public\"},"
+ "{\"success_action_redirect\": \"http://localhost/\"},"
+ "[\"starts-with\", \"$Content-Type\", \"\"],"
+ "[\"content-length-range\", 0, 1048576]" + "]" + "}";
public static String getPolicy() throws UnsupportedEncodingException {
return (new BASE64Encoder()).encode(policy_document.getBytes("UTF-8"))
.replaceAll("\n", "").replaceAll("\r", "");
}
public static String getSignature() throws InvalidKeyException,
IllegalStateException, NoSuchAlgorithmException,
UnsupportedEncodingException {
Mac hmac = Mac.getInstance("HmacSHA1");
hmac.init(new SecretKeySpec(awsSecretKey.getBytes("UTF-8"), "HmacSHA1"));
return (new BASE64Encoder()).encode(
hmac.doFinal(getPolicy().getBytes("UTF-8"))).replaceAll("\n",
"");
}
}
我的帖子表格看起来像这样
<form action="https://kanta-assets.s3.amazonaws.com/" method="post" enctype="multipart/form-data">
<input type="hidden" name="key" value="uploads/${filename}">
<input type="hidden" name="AWSAccessKeyId" value="@appconfig.Storage.awsAccessKey">
<input type="hidden" name="acl" value="public">
<input type="hidden" name="success_action_redirect" value="http://localhost/">
<input type="hidden" name="policy" value="@appconfig.Storage.getPolicy()">
<input type="hidden" name="signature" value="@appconfig.Storage.getSignature()">
<input type="hidden" name="Content-Type" value="image/jpeg">
<!-- Include any additional input fields here -->
File to upload to S3:
<input name="file" type="file">
<br>
<input type="submit" value="Upload File to S3">
</form>
如果我尝试上传 jpeg 图像,则会收到以下错误:
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<Error>
<Code>InvalidArgument</Code>
<Message/>
<ArgumentValue>public</ArgumentValue>
<ArgumentName>x-amz-acl</ArgumentName>
<RequestId>6679060CE7C84FA5</RequestId>
<HostId>
wSIzNZvFDFT7WNnUtBq9UY5WSN1ltN9dHErNk6L3v4ciZCSzwUgzTf1PgaFAJTWD
</HostId>
</Error>
我很确定我的错误是政策中的关键价值,"[\"starts-with\", \"$key\", \"\"],"
我不确定这实际上意味着什么。
对象键名的规则使用前缀字符串“upload/”,这意味着键值必须始终以upload/ 子目录路径开头。