我正在尝试允许浏览器上传到S3 Bucket(后端是PHP)经过大量的研究,我编辑了代码以正确创建GET请求的签名,但我仍然遇到POST和PUT请求的问题。
BackEnd代码:
$S3_KEY=$this->config->access_id;
$S3_SECRET=$this->config->secret_key;
$S3_BUCKET=$this->config->bucket_name;
$EXPIRE_TIME=(60 * 5); // 5 minutes
$S3_URL='https://'.$S3_BUCKET.'.s3.amazonaws.com';
$expires = time() + $EXPIRE_TIME;
$stringToSign = "POST\n\n\nmultipart/form-data;\n$expires\n/$S3_BUCKET/$objectName";
$sig = urlencode(base64_encode(hash_hmac('sha1', $stringToSign, $S3_SECRET,TRUE)));
$url = ($S3_URL."/".$objectName."?AWSAccessKeyId=".$S3_KEY."&Expires=".$expires."&Signature=".$sig);
return $url;
回应:
<Error><Code>SignatureDoesNotMatch</Code><Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message><StringToSignBytes>50 4f 53 54 0a 0a 6d 75 6c 74 69 70 61 72 74 2f 66 6f 72 6d 2d 64 61 74 61 3b 20 62 6f 75 6e 64 61 72 79 3d 2d 2d 2d 2d 57 65 62 4b 69 74 46 6f 72 6d 42 6f 75 6e 64 61 72 79 74 4d 41 6c 68 69 42 44 36 62 45 61 57 74 4f 37 0a 31 33 39 30 36 38 35 31 39 37 0a 2f 6e 61 64 69 74 65 73 74 2f 75 70 6c 6f 61 64 73 2f 37 38 35 33 32 32 2f 69 6d 61 67 65 73 2f 34 30 31 36 39 39 5f 31 30 31 35 31 31 37 30 38 37 32 31 31 35 33 33 38 5f 35 38 38 32 34 30 33 36 37 5f 6e 25 32 30 2d 25 32 30 43 6f 70 79 2e 6a 70 67</StringToSignBytes><RequestId>DB782E9EB8228AF3</RequestId><HostId>JOXpHXL8A/hiwYKmG7Qe8BZyjDXpipXgCUAcR6PghYydq4eYS01FHu23fiIN8t7k</HostId><SignatureProvided>A/Hq9w6+oekrBGKNxyPwiBZBna0=</SignatureProvided><StringToSign>POSTmultipart/form-data; boundary=----WebKitFormBoundarytMAlhiBD6bEaWtO71390685197/naditest/uploads/785322/images/401699_10151170872115338_588240367_n%20-%20Copy.jpg</StringToSign><AWSAccessKeyId>AKIAJTMMOZVOQUWZ33AQ</AWSAccessKeyId></Error>
我现在很茫然.......
答案 0 :(得分:0)
POST不支持查询字符串身份验证。
- http://docs.aws.amazon.com/AmazonS3/latest/dev/RESTAuthentication.html
部分原因似乎是您要在错误消息中找回的“要签名的字符串”:浏览器在请求中设置您无法预料的内容。
http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingHTTPPOST.html包含有关如何制作和签署“政策文件”以及使用表单帖子从浏览器上传文件的文档。该机制允许您比查询字符串身份验证机制提供的更多内容控制,包括指定成功和失败的重定向页面,acl和最小/最大文件大小。