这个问题让我有点发疯。我正在尝试通过 AJAX POST 将文件上传到 S3 存储桶。
我的所有凭据都是正确的,因为当我执行正常的 HTTP POST 时,它会在 S3 存储桶中创建资源就好了。但我真的很想用进度条一次上传多个文件,因此我需要 AJAX。
我的 S3 存储桶上有 CORS 设置:
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>http://localhost:3000</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
现在我只是想让它在我的开发环境(localhost:3000,使用标准 Rails 4.1)中工作。
根据我的理解,上面的 CORS 规则应该允许从 localhost:3000 到 S3 存储桶的 AJAX 请求。
但是,每次我通过 AJAX 提交文件时,都会出现以下错误:
XMLHttpRequest cannot load https://s3.amazonaws.com/<BUCKET>. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.
这对我来说没有任何意义,因为 localhost:3000 是通过 CORS 规则授予访问权限的。
我还提供了用于提交表单的 JS 片段:
$.ajax({
method: "POST",
crossDomain: true,
url: "https://s3.amazonaws.com/<BUCKET>",
data: $(this).serialize() # Contains S3 necessary values
})
该表单包含必要的 Amazon S3 key /等输入。我知道它们有效,因为当我执行正常的 HTTP POST 时,它会在 S3 中正确创建 Assets 。我想要做的就是 AJAXify 过程。
我在这里遗漏了一些明显的东西吗?
使用:Rails 4.1、jquery-file-upload、fog gem(用于 S3)
请您参考如下方法:
你可以尝试改变
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>Authorization</AllowedHeader>
</CORSRule>
</CORSConfiguration>


