1

我的 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/ 子目录路径开头。

4

2 回答 2

1

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

于 2012-08-19T23:10:49.860 回答
0

POST / HTTP/1.1 主机:destinationBucket.s3.amazonaws.com 用户代理:browser_data 接受:file_types 接受语言:区域接受编码:编码接受字符集:字符集保持活动:300 连接:保持活动内容类型:多部分/表单数据;边界=9431149156168 内容长度:长度

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

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

success_redirect --9431149156168 内容处置:表单数据;名称="内容类型"

content_type --9431149156168 内容处置:表单数据;名称="x-amz-meta-uuid"

uuid --9431149156168 内容处置:表单数据;名称="x-amz-元标签"

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

access-key-id --9431149156168 内容配置:表单数据;名称="政策"

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

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

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

上传到 Amazon S3 --9431149156168--

如何在java中创建发布请求?

于 2014-02-04T08:32:49.010 回答