【发布时间】:2017-05-07 22:29:16
【问题描述】:
前言:在 2013 年底,我的 Fineuploader 支持的上传网站正在使用 S3 作为存储。亚马逊随后在多处将授权机制升级为V4。所以我决定升级我的fineuploader。
我的问题是它没有发送x-amz-credential,而cors.sendCredentials 是真的。因此(新下载和调整的)PHP 服务器端显示undefined variable: credentialcondition。我们看一下配置和相关代码:
我将fineUploader配置为:
var s3Uploader = new qq.s3.FineUploader({
element: document.getElementById("fineuploader-s3"),
debug: true,
request: {
endpoint: "anonimized-bucket-name.s3.amazonaws.com",
accessKey: "AKIAANONIMIZEDG4AIYQ"
},
cors: {
expected: true,
sendCredentials: true
},
template: "qq-simple-thumbnails-template",
signature: {
endpoint: "index.php?mode=video&formId=3&hash=28120&v4=true"
},
uploadSuccess: {
endpoint: "index.php?mode=video&formId=3&hash=28120&v4=true&success=1"
},
iframeSupport: {
localBlankPagePath: "/include/blank.html"
},
chunking: {
enabled: true
},
resume: {
enabled: true
},
deleteFile: {
enabled: false,
method: "POST",
endpoint: "index.php?mode=video&formId=3&hash=28120&v4=true"
},
validation: {
itemLimit: 1000,
sizeLimit: 5000111000
},
thumbnails: {
placeholders: {
notAvailablePath: "fu/assets/not_available-generic.png",
waitingPath: "fu/assets/waiting-generic.png"
}
}
});
对我的 PHP 服务器的 POST 请求包含这个正文(添加了换行符以方便阅读):
{"expiration":"2016-12-22T09:59:32.767Z","conditions":[{"acl":"private"},
{"bucket":"anonimized-bucket-name"},{"Content-Type":"application/pdf"},
{"success_action_status":"200"},{"key":"c7e7c9dc-c4d3-4bec-af52-d3ad34c202ad.pdf"},
{"x-amz-meta-qqfilename":"Adatbazisok_2.pdf"},["content-length-range","0","5000111000"]]}
相关的服务器端代码是(与参考实现相比没有变化):
function signV4Policy($stringToSign, $policyObj) {
global $clientPrivateKey;
foreach ($policyObj["conditions"] as $condition) {
if (isset($condition["x-amz-credential"])) {
$credentialCondition = $condition["x-amz-credential"];
}
}
$pattern = "/.+\/(.+)\\/(.+)\/s3\/aws4_request/";
preg_match($pattern, $credentialCondition, $matches);
$dateKey = hash_hmac('sha256', $matches[1], 'AWS4' . $clientPrivateKey, true);
$dateRegionKey = hash_hmac('sha256', $matches[2], $dateKey, true);
$dateRegionServiceKey = hash_hmac('sha256', 's3', $dateRegionKey, true);
$signingKey = hash_hmac('sha256', 'aws4_request', $dateRegionServiceKey, true);
return hash_hmac('sha256', $stringToSign, $signingKey);
}
错误是在preg_match 的行,说undefined variable: cretentialCondition。
我收到的示例 policyObj:
array(2) {
["expiration"]=>
string(24) "2016-12-22T09:59:32.767Z"
["conditions"]=>
array(7) {
[0]=>
array(1) {
["acl"]=>
string(7) "private"
}
[1]=>
array(1) {
["bucket"]=>
string(12) "anonimized-bucket-name"
}
[2]=>
array(1) {
["Content-Type"]=>
string(15) "application/pdf"
}
[3]=>
array(1) {
["success_action_status"]=>
string(3) "200"
}
[4]=>
array(1) {
["key"]=>
string(40) "c7e7c9dc-c4d3-4bec-af52-d3ad34c202ad.pdf"
}
[5]=>
array(1) {
["x-amz-meta-qqfilename"]=>
string(17) "Adatbazisok_2.pdf"
}
[6]=>
array(3) {
[0]=>
string(20) "content-length-range"
[1]=>
string(1) "0"
[2]=>
string(10) "5000111000"
}
}
}
版本:php 7,fineuploader 5.11.0(最新),浏览器:Win 7 Firefox 50.1.0(最新)
【问题讨论】:
标签: javascript php amazon-s3 fine-uploader