javascript - AWS S3 存储桶上传时出现 CORS 错误

标签 javascript node.js amazon-web-services amazon-s3 cors

我的 AWS S3 存储桶 CORS 配置设置如下

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
        <AllowedOrigin>*</AllowedOrigin>
        <AllowedOrigin>http://localhost:5000</AllowedOrigin>
        <AllowedMethod>GET</AllowedMethod>
        <AllowedMethod>POST</AllowedMethod>
        <AllowedMethod>PUT</AllowedMethod>
        <AllowedHeader>*</AllowedHeader>
    </CORSRule>
</CORSConfiguration>

并在浏览器中发出签名的上传请求:

function upload_file(file, signed_request, url){
    var xhr = new XMLHttpRequest();
    xhr.open("PUT", signed_request);
    xhr.setRequestHeader('x-amz-acl', 'public-read');
    xhr.onload = function() {
        if (xhr.status === 200) {
            $('#photo').css('background-image', "url('"+url+"')");
        }
    };
    xhr.onerror = function(err) {
        alert("Could not upload file.", err);
    };
    xhr.send(file);
}

但是 chrome 和 firefox 都会抛出 Access-Control-Allow-Origin header not set 错误。特别是在 Firefox 中:“(原因:CORS header ‘Access-Control-Allow-Origin’丢失)。”

最佳答案

Access-Control-Allow-Headers header 不允许使用通配符。它必须包含实际的 header 名称。

在网络选项卡中查看预检请求。Access-Control-Request-Headers header 中的值必须包含为 AllowedHeader

关于javascript - AWS S3 存储桶上传时出现 CORS 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32511252/

相关文章:

javascript - 范围变量更改后的 Angular 更新指令

javascript - 如何在 ASP.NET MVC 中使用 Razor View 从对象列表中获取 Controller 中的单个对象

node.js - 在 ubuntu 12.04 上安装 nodejs 和 npm 后找不到 .npmrc 文件

node.js - Node.js async.parallel,它是并行的吗?

amazon-web-services - 将与模式匹配的Cloudwatch日志发送到SQS队列

javascript - div中加载了什么页面

javascript - Mongoose/MongoDB 创建集合(如果不存在则基于架构)

amazon-web-services - 如何在单个 API Gateway 后面合并多个微服务?

amazon-web-services - 在 AWS 上使用 Firebase Cloud Messaging (FCM) 发送通知时是否需要使用 Amazon SNS?

javascript - Bing map 在 Chrome 上显示,但在 IE 上不显示