我正在尝试从http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-post-example.html
重新创建示例第一步是将策略转换为base64。所以在这个例子中
{ "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" }
]
}
他们在base64中说这应该是
eyAiZXhwaXJhdGlvbiI6ICIyMDE1LTEyLTMwVDEyOjAwOjAwLjAwMFoiLA0KICAiY29uZGl0aW9ucyI6IFsNCiAgICB7ImJ1Y2tldCI6ICJzaWd2NGV4YW1wbGVidWNrZXQifSwNCiAgICBbInN0YXJ0cy13aXRoIiwgIiRrZXkiLCAidXNlci91c2VyMS8iXSwNCiAgICB7ImFjbCI6ICJwdWJsaWMtcmVhZCJ9LA0KICAgIHsic3VjY2Vzc19hY3Rpb25fcmVkaXJlY3QiOiAiaHR0cDovL3NpZ3Y0ZXhhbXBsZWJ1Y2tldC5zMy5hbWF6b25hd3MuY29tL3N1Y2Nlc3NmdWxfdXBsb2FkLmh0bWwifSwNCiAgICBbInN0YXJ0cy13aXRoIiwgIiRDb250ZW50LVR5cGUiLCAiaW1hZ2UvIl0sDQogICAgeyJ4LWFtei1tZXRhLXV1aWQiOiAiMTQzNjUxMjM2NTEyNzQifSwNCiAgICB7IngtYW16LXNlcnZlci1zaWRlLWVuY3J5cHRpb24iOiAiQUVTMjU2In0sDQogICAgWyJzdGFydHMtd2l0aCIsICIkeC1hbXotbWV0YS10YWciLCAiIl0sDQoNCiAgICB7IngtYW16LWNyZWRlbnRpYWwiOiAiQUtJQUlPU0ZPRE5ON0VYQU1QTEUvMjAxNTEyMjkvdXMtZWFzdC0xL3MzL2F3czRfcmVxdWVzdCJ9LA0KICAgIHsieC1hbXotYWxnb3JpdGhtIjogIkFXUzQtSE1BQy1TSEEyNTYifSwNCiAgICB7IngtYW16LWRhdGUiOiAiMjAxNTEyMjlUMDAwMDAwWiIgfQ0KICBdDQp9
使用以下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" +
"}";
final byte[] authBytes = policy_document.getBytes(StandardCharsets.UTF_8);
final String encoded = Base64.getEncoder().encodeToString(authBytes);
System.out.println("base64 " + encoded);
但我得到了(这是不同的)
eyAiZXhwaXJhdGlvbiI6ICIyMDE1LTEyLTMwVDEyOjAwOjAwLjAwMFoiLAogICJjb25kaXRpb25zIjogWwogICAgeyJidWNrZXQiOiAic2lndjRleGFtcGxlYnVja2V0In0sCiAgICBbInN0YXJ0cy13aXRoIiwgIiRrZXkiLCAidXNlci91c2VyMS8iXSwKICAgIHsiYWNsIjogInB1YmxpYy1yZWFkIn0sCiAgICB7InN1Y2Nlc3NfYWN0aW9uX3JlZGlyZWN0IjogImh0dHA6Ly9zaWd2NGV4YW1wbGVidWNrZXQuczMuYW1hem9uYXdzLmNvbS9zdWNjZXNzZnVsX3VwbG9hZC5odG1sIn0sCiAgICBbInN0YXJ0cy13aXRoIiwgIiRDb250ZW50LVR5cGUiLCAiaW1hZ2UvIl0sCiAgICB7IngtYW16LW1ldGEtdXVpZCI6ICIxNDM2NTEyMzY1MTI3NCJ9LAogICAgeyJ4LWFtei1zZXJ2ZXItc2lkZS1lbmNyeXB0aW9uIjogIkFFUzI1NiJ9LAogICAgWyJzdGFydHMtd2l0aCIsICIkeC1hbXotbWV0YS10YWciLCAiIl0sCgogICAgeyJ4LWFtei1jcmVkZW50aWFsIjogIkFLSUFJT1NGT0ROTjdFWEFNUExFLzIwMTUxMjI5L3VzLWVhc3QtMS9zMy9hd3M0X3JlcXVlc3QifSwKICAgIHsieC1hbXotYWxnb3JpdGhtIjogIkFXUzQtSE1BQy1TSEEyNTYifSwKICAgIHsieC1hbXotZGF0ZSI6ICIyMDE1MTIyOVQwMDAwMDBaIiB9CiAgXQp9
有什么想法吗?我真的从他们的页面复制并粘贴了该政策?没有这个,我无法重新创建这个例子。当我使用https://www.base64decode.org/解码两个签名时,它们返回相同的策略?那有什么不对?
答案 0 :(得分:2)
尝试使用此
final 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" + "}";
在用\ r \ n替换所有\ n之后,我获得了与AWS文档页面提供的相同的base64编码值。