gpt4 book ai didi

php - AWS 创建 POST 策略 base64 编码 + 签名

转载 作者:搜寻专家 更新时间:2023-10-31 21:32:16 24 4
gpt4 key购买 nike

我正在尝试在 PHP 中生成带有签名的 AWS POST 策略,该策略将被发送到客户端以允许通过 javascript AJAX 在浏览器中上传。

我已经在 http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-post-example.html 复制了这个例子

我有两个问题:

  1. 我无法根据策略生成正确编码的 base64 字符串。

    $policy = '{ "expiration": "2013-08-06T12:00:00.000Z", "conditions": [{"bucket": "examplebucket"}, ["starts-with", "$key", "user/user1/"], {"acl": "public-read"}, {"success_action_redirect": "http://acl6.s3.amazonaws.com/successful_upload.html"}, ["starts-with", "$Content-Type", "image/"], {"x-amz-meta-uuid": "14365123651274"}, ["starts-with", "$x-amz-meta-tag", ""], {"x-amz-credential": "AKIAIOSFODNN7EXAMPLE/20130806/us-east-1/s3/aws4_request"}, {"x-amz-algorithm": "AWS4-HMAC-SHA256"}, {"x-amz-date": "20130806T000000Z"}]}';
    $base64 = base64_encode($policy);
    //Result
    //eyAiZXhwaXJhdGlvbiI6ICIyMDEzLTA4LTA2VDEyOjAwOjAwLjAwMFoiLCAiY29uZGl0aW9ucyI6IFt7ImJ1Y2tldCI6ICJleGFtcGxlYnVja2V0In0sIFsic3RhcnRzLXdpdGgiLCAiJGtleSIsICJ1c2VyL3VzZXIxLyJdLCB7ImFjbCI6ICJwdWJsaWMtcmVhZCJ9LCB7InN1Y2Nlc3NfYWN0aW9uX3JlZGlyZWN0IjogImh0dHA6Ly9hY2w2LnMzLmFtYXpvbmF3cy5jb20vc3VjY2Vzc2Z1bF91cGxvYWQuaHRtbCJ9LCBbInN0YXJ0cy13aXRoIiwgIiRDb250ZW50LVR5cGUiLCAiaW1hZ2UvIl0sIHsieC1hbXotbWV0YS11dWlkIjogIjE0MzY1MTIzNjUxMjc0In0sIFsic3RhcnRzLXdpdGgiLCAiJHgtYW16LW1ldGEtdGFnIiwgIiJdLCB7IngtYW16LWNyZWRlbnRpYWwiOiAiQUtJQUlPU0ZPRE5ON0VYQU1QTEUvMjAxMzA4MDYvdXMtZWFzdC0xL3MzL2F3czRfcmVxdWVzdCJ9LCB7IngtYW16LWFsZ29yaXRobSI6ICJBV1M0LUhNQUMtU0hBMjU2In0sIHsieC1hbXotZGF0ZSI6ICIyMDEzMDgwNlQwMDAwMDBaIn1dfQ==

这是我的 utf8_encode() 策略。当我尝试对其进行 base64 编码时,它与示例 base64 策略不同。无论我做什么,似乎都无法让它匹配。我确实注意到将日期更改为 2013-08-07T12:00:00.000Z 使其与编码字符串的那部分匹配。

  1. 我无法使用正确编码的 base64 策略通过 sha256 生成正确的签名。

根据示例的 base64 编码策略创建签名:

    //Using this example secret key:
$secret = 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY';
$policy = 'eyAiZXhwaXJhdGlvbiI6ICIyMDEzLTA4LTA3VDEyOjAwOjAwLjAwMFoiLA0KICAiY29uZGl0aW9ucyI6IFsNCiAgICB7ImJ1Y2tldCI6ICJleGFtcGxlYnVja2V0In0sDQogICAgWyJzdGFydHMtd2l0aCIsICIka2V5IiwgInVzZXIvdXNlcjEvIl0sDQogICAgeyJhY2wiOiAicHVibGljLXJlYWQifSwNCiAgICB7InN1Y2Nlc3NfYWN0aW9uX3JlZGlyZWN0IjogImh0dHA6Ly9leGFtcGxlYnVja2V0LnMzLmFtYXpvbmF3cy5jb20vc3VjY2Vzc2Z1bF91cGxvYWQuaHRtbCJ9LA0KICAgIFsic3RhcnRzLXdpdGgiLCAiJENvbnRlbnQtVHlwZSIsICJpbWFnZS8iXSwNCiAgICB7IngtYW16LW1ldGEtdXVpZCI6ICIxNDM2NTEyMzY1MTI3NCJ9LA0KICAgIFsic3RhcnRzLXdpdGgiLCAiJHgtYW16LW1ldGEtdGFnIiwgIiJdLA0KDQogICAgeyJ4LWFtei1jcmVkZW50aWFsIjogIkFLSUFJT1NGT0ROTjdFWEFNUExFLzIwMTMwODA2L3VzLWVhc3QtMS9zMy9hd3M0X3JlcXVlc3QifSwNCiAgICB7IngtYW16LWFsZ29yaXRobSI6ICJBV1M0LUhNQUMtU0hBMjU2In0sDQogICAgeyJ4LWFtei1kYXRlIjogIjIwMTMwODA2VDAwMDAwMFoiIH0NCiAgXQ0KfQ==';


<?= hash_hmac('sha256', $policy, $secret); ?>

//Resulting Signature
d8ddc156c5d681b42c40a4224c07cdd64b938def8e8c34d616806175cb3c7119

//Signature in Example
21496b44de44ccb73d545f1a995c68214c9cb0d41c45a17a5daeec0b1a6db047

不确定我在这里遗漏了什么。我也有 PHP SDK,但我无法找到提取策略/签名的方法,因此我可以将其发送到浏览器中的 javascript。有没有办法从 PHP SDK 生成符合我指定条件的策略?我在网上和 SDK 中四处查看,结果一无所获...<​​/p>

最佳答案

我创建了我的签名

$policydoc = <<<EOT
{"expiration": "2016-01-01T00:00:00Z",
"conditions": [
{"bucket": "{$bucket}"},
["starts-with", "\$key", "uploads/"],
{"acl": "private"},
{"success_action_redirect": "$success_action_redirect"},
["content-length-range", 0, 1048576]
]
}
EOT;

$policydoc = base64_encode($policydoc);
$signature = base64_encode(hash_hmac('sha1', $policydoc, $provider->getSecretKey(), true));

我认为“sha1”在您的部分缺失。

关于php - AWS 创建 POST 策略 base64 编码 + 签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28204832/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com