使用php

时间:2015-12-06 10:39:53

标签: php amazon-web-services amazon-s3

我正在尝试使用php实现从浏览器到亚马逊s3的直接文件上传。但它表明Signature与错误不匹配。我的代码如下。 参考http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-authentication-HTTPPOST.html http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-post-example.html

$policy_arr = array (
              'expiration' => date('Y-m-d\TG:i:s\Z', strtotime('+20 hours')),
              'conditions' => 
                  array (array ( 'bucket' => 'bucket-name', ),
                         array ('starts-with', '$key', 'testupload.mp3',
                            ),
                        array (
                            'acl' => 'public-read',
                        ),
                        array (
                              'success_action_redirect' => 'http://s3.amazonaws.com/successful_upload.html',
                        ),

                        array (
                            'x-amz-credential' => 'AKIAHJI56EXAMPLE/20151204/us-west-2/s3/aws4_request',
                        ),
                        array (
                            'x-amz-algorithm' => 'AWS4-HMAC-SHA256',
                     ),
                        array (
                            'x-amz-date' =>  '20151204T120000Z',
                        ),
                    ),
                );


    $policy_string = json_encode($policy_arr);
    $string_to_sign = base64_encode($policy_string);

    $date_key = hash_hmac('sha256', 'AWS4dwqeqweqewrrrerwrerwrev0T/EXAMPKE+osNTFc', '20151204');
    $date_region_key = hash_hmac('sha256', $date_key, 'us-west-2');
    $date_region_service_key = hash_hmac('sha256', $date_region_key, 's3');
    $signing_string = hash_hmac('sha256', $date_region_service_key, 'aws4_request');

    $signature = hash_hmac('sha256', $signing_string, $string_to_sign);

<html>
  <head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  </head>
  <body>
  <form action="http://s3.amazonaws.com/" method="post" enctype="multipart/form-data">
<input type="input"  name="key" value="hypeddit-mfs/testupload.mp3" /><br />
    <input type="hidden" name="acl" value="public-read" />
    <input type="hidden" name="success_action_redirect" value="http://s3.amazonaws.com/successful_upload.html" />
    <input type="hidden" name="x-amz-meta-uuid" value="14365123651274" />
    <input type="text"   name="X-Amz-Credential" value="AKIAJUX56JHJSDEXAMPLE/20151204/us-west-2/s3/aws4_request" />
    <input type="text"   name="X-Amz-Algorithm" value="AWS4-HMAC-SHA256" />
    <input type="text"   name="X-Amz-Date" value="20151204T120000Z" />
    Tags for File: 
    <input type="input"  name="x-amz-meta-tag" value="" /><br />
    <input type="hidden" name="Policy" value="{{ $string_to_sign }}" />
    <input type="hidden" name="X-Amz-Signature" value="{{ $signature }}" />
    File: 
    <input type="file"   name="file" /> <br />
    <!-- The elements after this will be ignored -->
    <input type="submit" name="submit" value="Upload to Amazon S3" />
  </form>
</html>

0 个答案:

没有答案