【问题标题】:Java Amazon S3 Post formJava Amazon S3 Post 表单
【发布时间】:2012-08-19 23:00:30
【问题描述】:

我在使用 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/”, 这意味着键值必须始终以上传/开头 子目录路径。

【问题讨论】:

    标签: java html post amazon-s3


    【解决方案1】:

    尝试将密钥作为文件名。 (删除上传/)

    【讨论】:

    • 什么意思?我已经删除了上传/到"starts-with",""
    • 我的意思是在您的帖子表单中隐藏变量:- 删除上传/并使其成为 。如果你已经做到了,那么很好。我很久以前就遇到过类似的问题,我隐约记得有一个类似的修复方法。希望它可能会有所帮助。
    • 太好了,非常感谢!但我这边还有另一个错误。它必须类似于“public-read”而不是“public”。
    【解决方案2】:

    POST / HTTP/1.1 主机:destinationBucket.s3.amazonaws.com 用户代理:browser_data 接受:file_types 接受语言:地区 接受编码:编码 接受字符集:character_set 保活:300 连接:保持活动 内容类型:multipart/form-data;边界=9431149156168 内容长度:长度

    --9431149156168 内容处置:表单数据;名称=“键”

    acl --9431149156168 内容处置:表单数据; name="success_action_redirect"

    成功重定向 --9431149156168 内容处置:表单数据; name="内容类型"

    内容类型 --9431149156168 内容处置:表单数据; name="x-amz-meta-uuid"

    uuid --9431149156168 内容处置:表单数据; name="x-amz-meta-tag"

    元数据 --9431149156168 内容处置:表单数据; name="AWSAccessKeyId"

    访问密钥 ID --9431149156168 内容处置:表单数据; name="政策"

    编码策略 --9431149156168 内容处置:表单数据; name="签名"

    签名= --9431149156168 内容处置:表单数据;名称=“文件”;文件名="我的文件名.jpg" 内容类型:image/jpeg

    文件内容 --9431149156168 内容处置:表单数据;名称="提交"

    上传到 Amazon S3 --9431149156168--

    如何在 java 中创建 post 请求?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-17
      • 1970-01-01
      • 2013-01-28
      • 1970-01-01
      • 2011-07-05
      • 2012-10-23
      • 2012-01-14
      相关资源
      最近更新 更多