我的 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/