AWS的Base64Encode与Amazon示例

时间:2016-08-08 14:46:48

标签: amazon-web-services groovy

在我寻求基于Amazon S3浏览器的上传的第一道障碍中,我似乎失败了。

使用此处显示的亚马逊示例:http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-post-example.html

使用Groovy / Java,我无法对示例发布策略进行编码并生成与Amazon相同的编码。

{ "expiration": "2015-12-30T12:00:00.000Z",
  "conditions": [
    {"bucket": "sigv4examplebucket"},
    ["starts-with", "$key", "user/user1/"],
    {"acl": "public-read"},
    {"success_action_redirect": "http://sigv4examplebucket.s3.amazonaws.com/successful_upload.html"},
    ["starts-with", "$Content-Type", "image/"],
    {"x-amz-meta-uuid": "14365123651274"},
    {"x-amz-server-side-encryption": "AES256"},
    ["starts-with", "$x-amz-meta-tag", ""],

    {"x-amz-credential": "AKIAIOSFODNN7EXAMPLE/20151229/us-east-1/s3/aws4_request"},
    {"x-amz-algorithm": "AWS4-HMAC-SHA256"},
    {"x-amz-date": "20151229T000000Z" }
  ]
}

我把它定义为Groovy多行字符串:

String policy_document = '''
{ "expiration": "2015-12-30T12:00:00.000Z",
  "conditions": [
    {"bucket": "sigv4examplebucket"},
    ["starts-with", "$key", "user/user1/"],
    {"acl": "public-read"},
    {"success_action_redirect": "http://sigv4examplebucket.s3.amazonaws.com/successful_upload.html"},
    ["starts-with", "$Content-Type", "image/"],
    {"x-amz-meta-uuid": "14365123651274"},
    {"x-amz-server-side-encryption": "AES256"},
    ["starts-with", "$x-amz-meta-tag", ""],

    {"x-amz-credential": "AKIAIOSFODNN7EXAMPLE/20151229/us-east-1/s3/aws4_request"},
    {"x-amz-algorithm": "AWS4-HMAC-SHA256"},
    {"x-amz-date": "20151229T000000Z" }
  ]
}
'''

然后我用一个失败的断言进行编码和验证。

String base64Policy = (new BASE64Encoder()).encode(policy_document.replaceAll("\n", "").replaceAll("\r", "").getBytes("UTF-8"))
assert 'eyAiZXhwaXJhdGlvbiI6ICIyMDE1LTEyLTMwVDEyOjAwOjAwLjAwMF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NjUxMjM2NTEyNzQifSwNCiAgICB7IngtYW16LXNlcnZlci1zaWRlLWVuY3J5cHRpb24iOiAiQUVTMjU2In0sDQogICAgWyJzdGFydHMtd2l0aCIsICIkeC1hbXotbWV0YS10YWciLCAiIl0sDQoNCiAgICB7IngtYW16LWNyZWRlbnRpYWwiOiAiQUtJQUlPU0ZPRE5ON0VYQU1QTEUvMjAxNTEyMjkvdXMtZWFzdC0xL3MzL2F3czRfcmVxdWVzdCJ9LA0KICAgIHsieC1hbXotYWxnb3JpdGhtIjogIkFXUzQtSE1BQy1TSEEyNTYifSwNCiAgICB7IngtYW16LWRhdGUiOiAiMjAxNTEyMjlUMDAwMDAwWiIgfQ0KICBdDQp9

我希望一双新鲜的眼睛能指引我。非常感谢您提出的任何建议。

2 个答案:

答案 0 :(得分:0)

以下在Java中为我工作。将回车和换行保留在原位。

String policy_document = "{ \"expiration\": \"2015-12-30T12:00:00.000Z\",\n" +
            "  \"conditions\": [\n" +
            "    {\"bucket\": \"sigv4examplebucket\"},\n" +
            "    [\"starts-with\", \"$key\", \"user/user1/\"],\n" +
            "    {\"acl\": \"public-read\"},\n" +
            "    {\"success_action_redirect\": \"http://sigv4examplebucket.s3.amazonaws.com/successful_upload.html\"},\n" +
            "    [\"starts-with\", \"$Content-Type\", \"image/\"],\n" +
            "    {\"x-amz-meta-uuid\": \"14365123651274\"},\n" +
            "    {\"x-amz-server-side-encryption\": \"AES256\"},\n" +
            "    [\"starts-with\", \"$x-amz-meta-tag\", \"\"],\n" +
            "\n" +
            "    {\"x-amz-credential\": \"AKIAIOSFODNN7EXAMPLE/20151229/us-east-1/s3/aws4_request\"},\n" +
            "    {\"x-amz-algorithm\": \"AWS4-HMAC-SHA256\"},\n" +
            "    {\"x-amz-date\": \"20151229T000000Z\" }\n" +
            "  ]\n" +
            "}";
    String base64Policy = (new BASE64Encoder()).encode(policy_document.getBytes());

答案 1 :(得分:0)

感谢您指出我正确的方向。

最终的解决方案是在每一行的末尾按顺序添加\ r \ n。

String policy_document = "{ \"expiration\": \"2015-12-30T12:00:00.000Z\",\r\n" +
        "  \"conditions\": [\r\n" +
        "    {\"bucket\": \"sigv4examplebucket\"},\r\n" +
        "    [\"starts-with\", \"$key\", \"user/user1/\"],\r\n" +
        "    {\"acl\": \"public-read\"},\r\n" +
        "    {\"success_action_redirect\": \"http://sigv4examplebucket.s3.amazonaws.com/successful_upload.html\"},\r\n" +
        "    [\"starts-with\", \"$Content-Type\", \"image/\"],\r\n" +
        "    {\"x-amz-meta-uuid\": \"14365123651274\"},\r\n" +
        "    {\"x-amz-server-side-encryption\": \"AES256\"},\r\n" +
        "    [\"starts-with\", \"$x-amz-meta-tag\", \"\"],\r\n" +
        "\r\n" +
        "    {\"x-amz-credential\": \"AKIAIOSFODNN7EXAMPLE/20151229/us-east-1/s3/aws4_request\"},\r\n" +
        "    {\"x-amz-algorithm\": \"AWS4-HMAC-SHA256\"},\r\n" +
        "    {\"x-amz-date\": \"20151229T000000Z\" }\r\n" +
        "  ]\r\n" +
        "}";

不幸的是,它需要如此严格地实现,并且使用Groovy多行字符串无法实现此目的以保持标记更清晰。