gs://<your-cloud-storage-bucket> has no CORS configuration

喜夏-厌秋 提交于 2019-12-24 06:58:16

问题


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

https://firebase.google.com/docs/storage/web/download-files

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
 }
]

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.

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();
});
}

Error :

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.

Thanks.


回答1:


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

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>.



来源:https://stackoverflow.com/questions/44639212/gs-your-cloud-storage-bucket-has-no-cors-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!