Java Amazon S3 Post 表单

标签 java html post amazon-s3

我在使用 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/

相关文章:

java - JSF preRenderView 调用次数过多

jquery - 如何显示尖括号(包括方括号)之间写入的数据

javascript - getElementByID 不会将值传递给变量

javascript - 将 onclick 函数更改为 onload 函数时出错

c# - C# 中的 HTTPS POST 到证书错误的服务器 - 超时

post - Feign 无法使用 POST 方法

java - 覆盖父方法无法正常工作

java - 如何修复这个 Java 回文算法?

java - 给定一个字符串, "xyz"是否出现在字符串的中间?

ajax - 再次 CORS 头痛...对预检请求的响应未通过访问控制检查 : It does not have HTTP ok status