2

PhoneGap (Cordova) FileTransfer.upload() によって生成された CORS POST リクエストを介して S3 がアップロードを受け入れるようにするには、かなりの時間を費やしています。私が見逃している可能性があるものについての提案をいただければ幸いです。現在、以下のコードを使用して 403 AccessDenied 応答を取得しています。S3のドキュメントと比較して何度も調べましたが、問題がわかりません。

署名を生成する Python コードは次のとおりです。

# Create policy document for S3.
policy_obj = {"expiration": "2014-01-01T00:00:00Z",
  "conditions": [ 
    {"bucket": "<my.bucket.name>"}, 
    ["starts-with", "$key", "story_"],
    {"acl": "public-read"},
    ["eq", "$Content-Type", "audio/mp4"],
    ["content-length-range", str(0), str(2097152)]
  ]
}

policy = base64.b64encode(json.dumps(policy_obj))

# Create signature for S3
signature = base64.b64encode(
    hmac.new(
        key=app.config['AWS_SECRET_KEY'], 
        msg=policy, 
        digestmod=hashlib.sha1
    ).digest()
)

このプロセスによって生成された署名は、S3 Signature Testerによって生成された署名と一致します (base64 ポリシーを 16 進数に変換し、秘密鍵を使用して署名テスターを介して実行します)。

結果のポリシーと署名がクライアントに渡され、S3 へのリクエストがこの PhoneGap FileTransfer 呼び出しで構築されます。

// Upload file to Amazon S3
// r is the response object generated by Python
var options = new FileUploadOptions();
options.chunkedMode = false;
options.mimeType="audio/mp4";
options.fileKey='file';
options.fileName='story_' + uniqueKey + '.m4a';
options.params={
    "key": "${filename}",
    "acl": "public-read",
    "AWSAccessKeyId": r.aws_access_key,
    "Policy": r.policy,
    "Signature": r.signature,
};

var ft = new FileTransfer();
ft.upload(path, "http://<my.bucket.name>.s3.amazonaws.com/", uploadSuccess, uploadFail, options);

これは CORS 構成です (はい、アップロードが機能するようになったらロックダウンする予定です)。

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
        <AllowedOrigin>*</AllowedOrigin>
        <AllowedMethod>GET</AllowedMethod>
        <AllowedMethod>POST</AllowedMethod>
        <MaxAgeSeconds>3000</MaxAgeSeconds>
        <AllowedHeader>*</AllowedHeader>
    </CORSRule>
</CORSConfiguration>

これはバケット ポリシーです。

{
    "Version": "2008-10-17",
    "Id": "Policy1356975063803",
    "Statement": [
        {
            "Sid": "Stmt1357234455973",
            "Effect": "Allow",
            "Principal": {
                "AWS": "*"
            },
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::<my.bucket.name>/*"
        },
        {
            "Sid": "Stmt1356975061658",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::293855469575:root"
            },
            "Action": "s3:*",
            "Resource": "arn:aws:s3:::<my.bucket.name>"
        }
    ]
}

アップデート:

これは、Python がオブジェクトを JSON に変換した後のポリシー自体の外観です。

{
  "conditions": [
    {
      "bucket": "<my.bucket.name>"
    }, 
    [
      "starts-with", 
      "$key", 
      "story_"
    ], 
    {
      "acl": "public-read"
    }, 
    [
      "eq", 
      "$Content-Type", 
      "audio/mp4"
    ], 
    [
      "content-length-range", 
      "0", 
      "6291456"
    ]
  ], 
  "expiration": "2014-01-01T00:00:00Z"
}
4

1 に答える 1

0

Did you try removing newlines in your policy?

Here is the Ruby code for the policy, courtesy of s3_file_upload:

Base64.encode64(policy_data.to_json).gsub("\n", "")

Please let me know if you managed to get your upload to work, as I'm trying to do the same thing.

于 2013-02-10T22:44:29.293 に答える