本文介绍了gs://< your-cloud-storage-bucket>没有CORS配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在按钮单击事件中从Firebase存储器下载文件,但出现访问控制允许错误错误。

I am trying to download file from my firebase storage on button click event but it is giving me 'Access-Control-Allow-Origin' error.

按照上述链接,我正在尝试配置CORS。我安装了gsutil命令行工具。但是我找不到需要复制此代码的cors.json文件

As per above link I am trying to configure CORS. I installed the gsutil command line tool. But I am unable to find cors.json file where I need to copy this code

[
 {
 "origin": ["*"],
 "method": ["GET"],
 "maxAgeSeconds": 3600
 }
]

然后我使用命令 gsutil cors get gs://< your-cloud-storage-bucket> 会返回 gsutil cors得到gs://< your-cloud-storage-bucket> /没有CORS配置

I then used command gsutil cors get gs://<your-cloud-storage-bucket> which returns gsutil cors get gs://<your-cloud-storage-bucket>/ has no CORS configuration.

我是否需要先为存储桶创建CORS配置文件?

Do I need to create CORS configuration file for my storage bucket first ?

下面是我的按钮单击方法,以防错误出现在代码中。

Below is my button click method, incase the error is in the code.

downloadAttachment(fileName) {
var uid = AuthService.uid;
let storageRef = firebase.storage().ref().child('assignments/' + uid + '/' + fileName);
storageRef.getDownloadURL().then(url => {
  console.log(url);
  var xhr = new XMLHttpRequest();
  xhr.responseType = 'blob';
  xhr.onload = function (event) {
    var blob = xhr.response;
  };
  xhr.open('GET', url);
  xhr.send();
});
}

错误:

谢谢。

推荐答案

是的,创建一个名称为所需文件的文件-如果需要,可将其命名为 cors.json ,然后将首选的CORS配置设置放入其中,然后在 gsutil 上运行像这样:

Yes, create a file named anything you want—call it cors.json if you want—and put your preferred CORS config settings in it, and then run gsutil on it like this:

gsutil cors set cors.json gs://<your-cloud-storage-bucket>/

这会将这些CORS配置设置上传到您的存储桶中,然后您可以在以下位置检索当前设置随时使用 gsutil cors获取gs://< your-cloud-storage-bucket>

That will upload those CORS config settings to your bucket, and then after that you can retrieve the current settings at any time using gsutil cors get gs://<your-cloud-storage-bucket>.

这篇关于gs://&lt; your-cloud-storage-bucket&gt;没有CORS配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 00:23