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

时间:2015-01-29 00:07:40

标签: php amazon-web-services sdk signature policy

我正在尝试使用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==
    
  2. 这是我的utf8_encode()政策。当我尝试base64编码时,它与示例base64策略不同。无论我做什么,似乎无法让它匹配。我注意到将日期更改为2013-08-07T12:00:00.000Z使其与编码字符串的那一部分匹配。

    1. 我无法使用正确编码的base64策略通过sha256生成正确的签名。
    2. 从示例的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本身并且空了......

1 个答案:

答案 0 :(得分:1)

我用

创建了我的签名
Public Async Function WriteAllBytesAsync(path As String, data() As Byte) As Task
    Await Task.Run(
         Sub()
             File.WriteAllBytes(path, data)
         End Sub)
End Function

我认为' sha1 '缺少你的部分。