上传到S3时出现错误的XMLHttpRequest

时间:2017-07-28 17:22:21

标签: javascript amazon-web-services encryption amazon-s3

我正在使用Evaporate.js将文件上传到S3。在我决定启用服务器端加密之前,我已经完成了所有工作。

根据S3文档,您可以通过传递标头来启用它。所以我更新了我的添加代码,如下所示:

var promise = _e_.add({
            name: name,
            file: files[i],
            started: callback_methods.started,
            complete: callback_methods.complete,
            cancelled: callback_methods.cancelled,
            progress: callback_methods.progress,
            error: callback_methods.error,
            warn: callback_methods.warn,
            paused: callback_methods.paused,
            pausing: callback_methods.pausing,
            resumed: callback_methods.resumed,
            nameChanged: callback_methods.nameChanged,
            xAmzHeadersAtInitiate: { 'x-amz-server-side​-encryption': 'AES256'} // THIS IS THE ONLY LINE THAT CHANGED!!!
          }
          )

我收到错误: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.

1 个答案:

答案 0 :(得分:6)

<强>更新

标题字段只能是ASCII字符。代码中的x-amz-server-side-encryption包含隐藏的字符。键入它而不是从某处复制粘贴它。转到此web page并在复制问题后粘贴标题字段名称,您将看到我的意思。

来自documentation

  

使用预先签名的网址上传对象时,无法强制执行SSE-S3加密对象。

您需要将标题与URL一起签名。只需在签署URL后发送标题即可。

var promise = _e_.add({
    name: name,
    file: files[i],
    started: callback_methods.started,
    complete: callback_methods.complete,
    cancelled: callback_methods.cancelled,
    progress: callback_methods.progress,
    error: callback_methods.error,
    warn: callback_methods.warn,
    paused: callback_methods.paused,
    pausing: callback_methods.pausing,
    resumed: callback_methods.resumed,
    nameChanged: callback_methods.nameChanged,
    signHeaders: { 'x-amz-server-side-encryption': 'AES256' }, // notice this
    xAmzHeadersAtInitiate: { 'x-amz-server-side-encryption': 'AES256'} // this should be fine now as we have the header in the signed request too but try removing this if you still get an error. S3 does not require you to re-specify the headers that were already signed.
});