无法在'XMLHttpRequest'上执行'setRequestHeader':'AWS4-HMAC-SHA256(Node JS)

时间:2018-11-12 18:08:10

标签: node.js amazon-web-services amazon-s3 multipart

我正在尝试使用EvaporateJS将文件上传到S3存储桶。但是当我尝试上传文件时,我总是收到错误消息。

错误消息:

DOMException: Failed to execute 'setRequestHeader' on 
'XMLHttpRequest': 'AWS4-HMAC-SHA256 
Credential=XXXXXXXXXXXXXXX/XXXXXXX/us-east-1/s3/aws4_request, 
SignedHeaders=content-type;host;x-amz-date;x-amz-server-side​- 
encryption, Signature=XXXXXXXXXXXXXXXXXXXXX' is not a valid HTTP 
header field value.

我知道这个问题类似于this。我尝试过这种解决方案,但并不能解决我的错误。

这是我的代码,

Evaporate.create({
    signerUrl: this.signingUrl,
    aws_key: 'XXXXXXXXXXXX',
    bucket: 'bucket-name',
    awsSignatureVersion: '4',
    computeContentMd5: true,
    xAmzHeadersAtInitiate: { 'x-amz-acl': 'public-read' },
    cryptoMd5Method: function (data) { return AWS.util.crypto.createHash('md5').update(data).digest('base64'); },
    cryptoHexEncodedHash256: function (data) { return AWS.util.crypto.sha256(data, 'hex'); }
})
    .then(
        // Successfully created evaporate instance `_e_`
        function success(_e_) {
            var filePromises = [];
            // Start a new evaporate upload anytime new files are added in the file input

            var promise = _e_.add({
                name: 'test_' + Math.floor(1000000000 * Math.random()),
                file: files,
                //signHeaders: { 'x-amz-server-side-encryption': 'AES256' },

                progress: function (progress) {
                    console.log('making progress: ' + progress);
                }
            })
                .then(function (awsKey) {
                    console.log(awsKey, 'complete!');
                });
            filePromises.push(promise);

            // Wait until all promises are complete
            Promise.all(filePromises)
                .then(function () {
                    console.log("file promise ", filePromises)
                    console.log('All files were uploaded successfully.');
                }, function (reason) {
                    console.log('All files were not uploaded successfully:', reason);
                });

        },
        // Failed to create new instance of evaporate
        function failure(reason) {
            console.log('Evaporate failed to initialize: ', reason)
        }

S3存储桶CORS配置

<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>PUT</AllowedMethod>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<ExposeHeader>x-amz-server-side-encryption</ExposeHeader>
<ExposeHeader>x-amz-request-id</ExposeHeader>
<ExposeHeader>x-amz-id-2</ExposeHeader>
<ExposeHeader>ETag</ExposeHeader>
<AllowedHeader>*</AllowedHeader>

谢谢。

0 个答案:

没有答案