Firebase存储和访问控制 - 允许来源 [英] Firebase Storage and Access-Control-Allow-Origin
问题描述
我试图通过XMLHttpRequest从Firebase存储中下载文件,但Access-Control-Allow-Origin没有在资源上设置,所以这是不可能的。有没有什么办法可以在存储服务器上设置这个头文件?
(let [xhr(js / XMLHttpRequest。)]
(.open xhrGETurl)
(aset xhrresponseTypearraybuffer)
(aset xhronload#(js / console.logbin )))
(.send xhr)))
Chrome出错信息:
XMLHttpRequest无法加载
https:// firebasestorage.googleapis.com/
请求的
资源上没有Access-Control-Allow-Origin标题。 Origin' http:// localhost:3449 '因此是不允许的
访问。
从在firebase-talk组/列表上发帖:
lockquote>
为CORS配置数据最简单的方法是使用 gsutil
命令行工具。
gsutil
的安装说明可在 https:/ /cloud.google.com/storage/docs/gsutil_install 。
安装 gsutil
并通过验证后,就可以使用它来配置CORS。
例如,如果您只想从自定义域中允许对象下载,请将此数据放在名为cors.json的文件中(替换https://example.com
与您的域名):
[
然后运行这个命令(用你的名字替换
{
origin:[https://
method:[GET],
maxAgeSeconds:3600
}
]
exampleproject.appspot.com
桶):
gsutil cors set cors.json gs://exampleproject.appspot.com
$ c
$ b你应该设置。
如果你需要更复杂的CORS配置请参阅 https://cloud.google.com/存储/文档/交叉ORI gin#配置CORS-a-Bucket 。
I'm trying to download files from Firebase Storage through a XMLHttpRequest, but Access-Control-Allow-Origin is not set on the resource, so it's not possible. Is there any way to set this header on the storage server?
(let [xhr (js/XMLHttpRequest.)] (.open xhr "GET" url) (aset xhr "responseType" "arraybuffer") (aset xhr "onload" #(js/console.log "bin" (.-response xhr))) (.send xhr)))
Chrome error message:
XMLHttpRequest cannot load https://firebasestorage.googleapis.com/[EDITED] No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3449' is therefore not allowed access.
解决方案From this post on the firebase-talk group/list:
The easiest way to configure your data for CORS is with the
gsutil
command line tool. The installation instructions forgsutil
are available at https://cloud.google.com/storage/docs/gsutil_install. Once you've installedgsutil
and authenticated with it, you can use it to configure CORS.For example, if you just want to allow object downloads from your custom domain, put this data in a file named cors.json (replacing
"https://example.com"
with your domain):[ { "origin": ["https://example.com"], "method": ["GET"], "maxAgeSeconds": 3600 } ]
Then, run this command (replacing
"exampleproject.appspot.com"
with the name of your bucket):gsutil cors set cors.json gs://exampleproject.appspot.com
and you should be set.
If you need a more complicated CORS configuration, check out the docs at https://cloud.google.com/storage/docs/cross-origin#Configuring-CORS-on-a-Bucket.
这篇关于Firebase存储和访问控制 - 允许来源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!