简体   繁体   English

上传到S3时出现错误的XMLHttpRequest

[英]Bad XMLHttpRequest when uploading to S3

I'm using Evaporate.js to upload files to S3. 我正在使用Evaporate.js将文件上传到S3。 I've had everything working, until I decided to enable server side encryption. 在我决定启用服务器端加密之前,我已经完成了所有工作。

According to the S3 docs, you can enable it by passing a header. 根据S3文档,您可以通过传递标头来启用它。 So I updated my add code to look like: 所以我更新了我的添加代码,如下所示:

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!!!
          }
          )

I get the error: 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. 我收到错误: 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.

Update: 更新:

Header fields can only be ASCII characters. 标题字段只能是ASCII字符。 x-amz-server-side-encryption in your code contains a hidden character. x-amz-server-side-encryption包含隐藏字符。 Type it instead of copy pasting it from somewhere. 键入它而不是从某处复制粘贴它。 Go to this web page and paste the header field name after copying from your question, you will see what i mean. 从您的问题复制后,转到此网页并粘贴标题字段名称,您将看到我的意思。

From the documentation : 文档

You can't enforce whether or not objects are encrypted with SSE-S3 when they are uploaded using pre-signed URLs. 使用预先签名的URL上载对象时,无法强制执行是否使用SSE-S3加密对象。

You need to sign the header along with the URL. 您需要将标题与URL一起签名。 Just sending the headers after signing the URL won't work. 只是在签署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.
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM