带有大文件的精简上传器S3失败

时间:2014-12-12 18:16:43

标签: fine-uploader

我正在尝试使用精细上传器s3上传高达500MB的文件。 版本是5.0.9。 这是我使用的代码。

function getFineUploaderOptions() {

    var options = {
        debug: true,
        template: "qq-template-manual-noedit",
        autoUpload: false,
        multiple: false,
        request: {
            endpoint: UploadVars.endpoint,
            accessKey: UploadVars.accessKey
        },
        signature: {
            endpoint:   "entry_essay_handler_s3.php"
        },
        uploadSuccess: {
            endpoint:   "entry_essay_handler_s3.php?success"
        },
        // required if non-File-API browsers, such as IE9 and older, are used
        iframeSupport: {
            localBlankPagePath: 'blank.html'
        },
        validation: {
            allowedExtensions: UploadVars.allowedExtensions,
            sizeLimit: UploadVars.sizeLimit
        },
        showMessage: function(message) {
            // Using Twitter Bootstrap's classes and jQuery selector and method
            console.log(message);
            $('#imageUploadmsg').html(message);
            $('#imageUploadmsg').css('visibility','visible');
        },
        objectProperties: {
            key: function (id) {
                return getUploadPath(id);
            }
        },
        chunking: {
            enabled: true

        },
        retry: {
            enableAuto: true
        },
        resume: {
            enabled: true
        },
        messages:{
            unsupportedBrowser: "You need to update your browser in order to upload a file."
        }
    };

    return options;
}

这是uploadVars变量。

 var UploadVars = {

  accessKey: 'somevalue',
  endpoint: "http://s3.amazonaws.com/somevalue",
  allowedExtensions: ['zip', 'rar'],
  izeLimit: '524288000',

};

在上传按钮上我使用了这段代码。

$('#imageUpload').fineUploaderS3(getFineUploaderOptions())

    .on('upload', function(event, id, name) {

    })
    .on('submit', function(event, id, name) {

    })
    .on('complete', function(event, id, name, json, xhr) {


    })
    .on('cancel', function(event, id, name, json, xhr) {

    })
    .on('autoRetry', function (event, id, name, attemptNumber) {
        // leave the code for auto Retry
    })
    .on('error', function(event, id, name, errorReason, xhr) {

    });

我总是得到这些错误。 [Fine Uploader 5.0.9]尝试解析签名响应时出错: SyntaxError:意外的令牌

custom.fineuploader-5.0.9.js:212 [Fine Uploader 5.0.9]从服务器收到空的或无效的响应!

我确认它适用于小尺寸文件。

这是S3 CORS设置。

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
        <AllowedOrigin>*</AllowedOrigin>
        <AllowedMethod>POST</AllowedMethod>
        <AllowedMethod>PUT</AllowedMethod>
        <AllowedMethod>DELETE</AllowedMethod>
        <MaxAgeSeconds>3000</MaxAgeSeconds>
        <ExposeHeader>ETag</ExposeHeader>
        <AllowedHeader>content-type</AllowedHeader>
        <AllowedHeader>origin</AllowedHeader>
        <AllowedHeader>x-amz-acl</AllowedHeader>
        <AllowedHeader>x-amz-meta-qqfilename</AllowedHeader>
        <AllowedHeader>x-amz-date</AllowedHeader>
        <AllowedHeader>authorization</AllowedHeader>
    </CORSRule>
</CORSConfiguration>

我还更新了php.ini文件upload_max_filesize = 500MB,post_max_size = 600M 和phpinfo确认我指定了正确的尺寸。

这是php方面的签名过程。

function signRequest() {
    header('Content-Type: application/json');

    $responseBody = file_get_contents('php://input');
    $contentAsObject = json_decode($responseBody, true);
    $jsonContent = json_encode($contentAsObject);

    $headersStr = null;
    if (isset($contentAsObject["headers"]))
        $headersStr = $contentAsObject["headers"];


    if ($headersStr) {
        return signRestRequest($headersStr);
    }
    else {

        return signPolicy($jsonContent);

    }
}

function signRestRequest($headersStr) {
    if (isValidRestRequest($headersStr)) {
        $response = array('signature' => sign($headersStr));
    }
    else {

        logMessage("Signin is failed. Your file could not be uploaded at this time. please try again later");

        echo json_encode(array("invalid" => true, "success" => false));
        return false;
    }
    return true;
}

function isValidRestRequest($headersStr) {
    global $expectedBucketName;

    $pattern = "/\/$expectedBucketName\/.+$/";
    preg_match($pattern, $headersStr, $matches);

    return count($matches) > 0;
}

function signPolicy($policyStr) {
    $policyObj = json_decode($policyStr, true);


    if (isPolicyValid($policyObj)) {

        $encodedPolicy = base64_encode($policyStr);
        $response = array('policy' => $encodedPolicy, 'signature' => sign($encodedPolicy));
        echo json_encode($response);
    }
    else {

        logMessage("Your file could not be uploaded at this time. please try again later");

        echo json_encode(array("invalid" => true, "success" => false));
        return false;
    }
    return true;
}

function isPolicyValid($policy) {
    global $expectedMaxSize, $expectedBucketName;

    $conditions = $policy["conditions"];

    $bucket = null;
    $parsedMaxSize = null;

    for ($i = 0; $i < count($conditions); ++$i) {
        $condition = $conditions[$i];

        if (isset($condition["bucket"])) {
            $bucket = $condition["bucket"];
        }
        else if (isset($condition[0]) && $condition[0] == "content-length-range") {
            $parsedMaxSize = $condition[2];
        }
    }

    return $bucket == $expectedBucketName && $parsedMaxSize == (string)$expectedMaxSize;
}

function sign($stringToSign) {
    global $clientPrivateKey;

    return base64_encode(hash_hmac(
        'sha1',
        $stringToSign,
        $clientPrivateKey,
        true
    ));
}

有人可以帮我解决这个问题吗? 感谢

1 个答案:

答案 0 :(得分:0)

您的签名服务器返回对Fine Uploader签名请求的无效响应。 Fine Uploader期望有效的JSON响应,但您的服务器返回HTML后跟JSON字符串。例如,在签名JSON字符串之前,您的服务器将其包含在签名响应中:

<br />
<b>Deprecated</b>:  mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in <b>/Applications/MAMP/htdocs/InSite_dev/wwwroot/1620/common/config/config.php</b> on line <b>18</b><br />
<br />
<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/InSite_dev/wwwroot/1620/common/config/config.php:18) in <b>/Applications/MAMP/htdocs/InSite_dev/wwwroot/1620/cookiehandler.php</b> on line <b>16</b><br />
<br />
<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/InSite_dev/wwwroot/1620/common/config/config.php:18) in <b>/Applications/MAMP/htdocs/InSite_dev/wwwroot/1620/cookiehandler.php</b> on line <b>32</b><br />
<br />
<b>Warning</b>:  session_start(): Cannot send session cache limiter - headers already sent (output started at /Applications/MAMP/htdocs/InSite_dev/wwwroot/1620/common/config/config.php:18) in <b>/Applications/MAMP/htdocs/InSite_dev/wwwroot/1620/entry_essay_handler_s3.php</b> on line <b>20</b><br />
<br />
<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/InSite_dev/wwwroot/1620/common/config/config.php:18) in <b>/Applications/MAMP/htdocs/InSite_dev/wwwroot/1620/entry_essay_handler_s3.php</b> on line <b>370</b><br />