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 Accept: file_types Accept-Language: Regions Accept-Encoding: encoding Accept-Charset: character_set Keep-Alive: 300 接続: keep-alive Content-Type : マルチパート/フォームデータ; 境界 = 9431149156168 コンテンツの長さ: 長さ

--9431149156168 Content-Disposition: フォームデータ。名前="キー"

acl --9431149156168 Content-Disposition: フォームデータ。name="success_action_redirect"

success_redirect --9431149156168 Content-Disposition: フォーム データ。name="コンテンツタイプ"

content_type --9431149156168 Content-Disposition: フォームデータ; name="x-amz-meta-uuid"

uuid --9431149156168 Content-Disposition: フォームデータ。name="x-amz-meta-tag"

メタデータ --9431149156168 Content-Disposition: フォームデータ。name="AWSAccessKeyId"

access-key-id --9431149156168 Content-Disposition: フォームデータ。名前="ポリシー"

encoded_policy --9431149156168 Content-Disposition: フォームデータ。名前="署名"

signature= --9431149156168 Content-Disposition: フォームデータ。名前="ファイル"; filename="MyFilename.jpg" コンテンツ タイプ: image/jpeg

file_content --9431149156168 Content-Disposition: フォームデータ; 名前="送信"

Amazon S3 にアップロード --9431149156168--

Javaで投稿リクエストを作成するには?

于 2014-02-04T08:32:49.010 に答える