evaporate.js s3直接上传403

时间:2018-04-04 11:40:36

标签: file amazon-web-services amazon-s3 file-upload evaporate.js

我正在尝试使用蒸发js将文件直接上传到浏览器到S3。 我按照jqueryajaxphp.com上的教程进行了操作,但我遇到了签名问题

Signature.php

<?php

$to_sign = $_GET['to_sign'];
$secret = 'AWS_SECRET';
$hmac_sha1 = hash_hmac('sha1', $to_sign, $secret, true);
$signature = base64_encode($hmac_sha1);
echo $signature;

上传功能

function largeFileUPload(file) {

    var ins = new Evaporate({
        signerUrl: './includes/s3-signature.php',
        aws_key: "AWS_KEY_XXXXXXX",
        bucket: 'bucket-name',
        awsRegion: 'eu-west-1',
        cloudfront: true,
        aws_url: 'http://bucket-name.s3-accelerate.amazonaws.com',
        // partSize: 10 * 1024 * 1024,
        s3Acceleration: true,
        computeContentMd5: true,
        cryptoMd5Method: function (data) { return AWS.util.crypto.md5(data, 'base64'); },
        cryptoHexEncodedHash256: function (data) { return AWS.util.crypto.sha256(data, 'hex'); }
    });
    // http://<?=$my_bucket?>.s3-<?=$region?>.amazonaws.com

    ins.add({
        name: 'evaporateTest' + Math.floor(1000000000*Math.random()) + '.' + file.name.replace(/^.*\./, ''),
        file: file,
        xAmzHeadersAtInitiate : {
            'x-amz-acl': 'public-read'
        },
        signParams: {
            foo: 'bar'
        },
        complete: function(r){
            console.log('Upload complete');
        },
        progress: function(progress){
            var progress = Math.floor(progress*100);
            console.log(progress);
        },
        error: function(msg){
            console.log(msg);
        }
    });
}

我将接受来自s3终点的回复

<Code>SignatureDoesNotMatch</Code>
<Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message>

我也尝试过blueimp插件,但是文件超过200MB

会失败

1 个答案:

答案 0 :(得分:0)

来自jqueryajaxphp.com的教程中的Signature.php文件导致了问题。我通过evaporate.js文档修改了php签名文件,它解决了问题

<强> Signature.php

$to_sign = $_GET['to_sign'];
$secret = 'AWS_SECRET';

$formattedDate = substr($dateTime, 0, 8);

//make the Signature, notice that we use env for saving AWS keys and regions
$kSecret = "AWS4" . $secret;
$kDate = hash_hmac("sha256", $formattedDate, $kSecret, true);
$kRegion = hash_hmac("sha256", "eu-west-1", $kDate, true);
$kService = hash_hmac("sha256", 's3', $kRegion, true);
$kSigning = hash_hmac("sha256", "aws4_request", $kService, true);
$signature = hash_hmac("sha256", $to_sign, $kSigning);

echo $signature;