我在使用 s3 post 表单时遇到问题。我读过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\",\"\"],"
我不确定这实际上意味着什么。
The rule for the object’s key name uses the prefix string “upload/”, which means that the key value must always start with the upload/ subdirectory path.
最佳答案
尝试将 key 作为文件名。 (删除上传/)
关于Java Amazon S3 Post 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12030653/