上载到S3时XMLHttpRequest错误 [英] Bad XMLHttpRequest when uploading to S3

查看:111
本文介绍了上载到S3时XMLHttpRequest错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Evaporate.js to upload files to S3. I've had everything working, until I decided to enable server side encryption.

根据S3文档,您可以通过传递标头来启用它.因此,我将添加代码更新为:

According to the S3 docs, you can enable it by passing a header. 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!!!
          }
          )

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

推荐答案

更新:

标题字段只能是ASCII字符.您的代码中的x-amz-server-side-encryption包含一个隐藏字符.键入它,而不是从某个地方复制粘贴它.从您的问题复制后,转到此网页并粘贴标题字段名称,您将看到卑鄙的.

Header fields can only be ASCII characters. x-amz-server-side-encryption in your code contains a hidden character. 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.

文档:

使用预先签名的URL上传对象时,您不能强制对象是否用SSE-S3加密.

You can't enforce whether or not objects are encrypted with SSE-S3 when they are uploaded using pre-signed URLs.

您需要在标题和URL上签名.仅在签名URL后发送标头将不起作用.

You need to sign the header along with the URL. Just sending the headers after signing the URL won't work.

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.
});

这篇关于上载到S3时XMLHttpRequest错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆