简体   繁体   English

如何从客户端获取 firebase 存储 json 文件

[英]how to fetch firebase storage json file from client side

I saved a json file in google cloud storage, I want to fetch the file, read the json content and save the content to client indexeddb.我在谷歌云存储中保存了一个json文件,我想获取文件,读取json内容并将内容保存到客户端indexeddb。

loadJSON(function(json) {
   console.log(json); // this will log out the json object
 });

var fileUrl ="https://firebasestorage.googleapis.com/v0/b/rwrwr-rwr.appspot.com/o/TestCatalog.json?alt=media&token=r545-54-54-54-45455"


function loadJSON() {

  var request = new Request(fileUrl);

  fetch(request, {mode: 'cors'}).then(function(response) {
    // Convert to JSON
    return response.json();
   }).then(function(j) {
  // Yay, `j` is a JavaScript object
   console.log(JSON.stringify(j));
  }).catch(function(error) {
   console.log('Request failed', error)
  });
  }

The fetch is producing this error提取正在产生此错误

from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. CORS 策略已阻止来自 origin 'null' 的请求:请求的资源上不存在 'Access-Control-Allow-Origin' 标头。 If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.如果不透明响应满足您的需求,请将请求的模式设置为“no-cors”以在禁用 CORS 的情况下获取资源。

How could I fetch the firebase storage json file ?我怎样才能获取 firebase 存储 json 文件?

If you're running this code form the browser you have to enable cross-origin access on your Cloud Storage bucket.如果您在浏览器中运行此代码,则必须在 Cloud Storage 存储分区上启用跨域访问。
For that you can use the gsutil tool und run为此,您可以使用gsutil工具并运行

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

The cors.json file should contain the following content: cors.json 文件应包含以下内容:

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

You can find the documentation on that here .您可以在此处找到相关文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从谷歌 Firebase 存储中获取 json 文件 - How to fetch json file from google firebase storage "如何从客户端 React 应用程序使用 Firebase 存储?" - How do I use firebase storage from a client side React app? 如何从服务器端javascript从不同的域获取.txt文件并获取它的客户端 - How to fetch a .txt file from a different domain from server side javascript and get it client side 无法通过 fetch 从客户端调用 Firebase Cloud Function - Unable to call Firebase Cloud Function from client-side with fetch 从 firebase 存储下载 json 文件 - Download json file from firebase storage Firebase:从存储中获取 JSON 文件的内容 - Firebase: Get contents of JSON file from storage 从客户端访问 Firebase 云存储中的文件时,使用 downloadURL 与 getBlob 获取文件是否更划算? - When accessing files in Firebase Cloud Storage from a client, is it more cost effective to fetch a file using a downloadURL vs getBlob? Firebase/Angular:从 Firebase 存储读取 JSON 文件 - Firebase/Angular : read JSON file from Firebase Storage 如何使用JavaScript在客户端创建存储文件? - How can I create a file for storage on the client side with JavaScript? 如何从此 JSON 文件中获取? - How to fetch from this JSON file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM