amazon-s3 - Rails 5.2 Active Storage直接上传失败: CORS header ‘Access-Control-Allow-Origin’ missing

标签 amazon-s3 rails-activestorage ruby-on-rails-5.2

我正在尝试使用Rail 5.2.0.rc1 Active Storage,使用其随附的JavaScript库将PDF文档直接从客户端上传到云。
但是在提交表单时,Firefox和Chrome都出现浏览器错误:

Cross-Origin Request Blocked... (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

这样就设置了 header ,从而在activestorage.js中设置了this.xhr.setRequestHeader ...,但似乎不是可配置的:https://github.com/rails/rails/blob/master/activestorage/app/javascript/activestorage/blob_record.js

有任何建议吗?

最佳答案

尝试使用以下命令在S3存储桶上设置CORS权限:

<?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>
<CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>PUT</AllowedMethod>
    <AllowedMethod>POST</AllowedMethod>
    <MaxAgeSeconds>3000</MaxAgeSeconds>
    <AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>

关于amazon-s3 - Rails 5.2 Active Storage直接上传失败: CORS header ‘Access-Control-Allow-Origin’ missing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49385431/

相关文章:

ruby-on-rails - Rails 5.2凭证+ Assets 预编译

ruby-on-rails - 如何使用 Active Storage 存储来自 URL 的图像

rich-text-editor - 有什么办法可以在 Rails 6 中禁用 Action 文本的图像/附件部分吗?

ruby-on-rails - 如何从 Rails API 返回图像?

ruby-on-rails - rails 5.2 : form_with requires "local: false" for AJAX requests?

amazon-s3 - S3 预签名下载 url 立即过期,为什么?

amazon-s3 - 使用 s3export 将数据从 Vertica 导出到 S3 时遇到问题

amazon-web-services - Amazon Cloudfront 允许所有备用域名 (CNAMES)

amazon-s3 - 当对象上传到 S3 存储桶时,如何触发 AWS Cloudformation 堆栈的更新?

ruby-on-rails - 变体委托(delegate)给附件,但附件为零