【问题标题】:Firebase: Get contents of JSON file from storageFirebase:从存储中获取 JSON 文件的内容
【发布时间】:2021-08-30 16:50:17
【问题描述】:

我正在尝试“下载”我存储在我的 firebase 中的 JSON 文件的内容,以便能够在我的 google 操作中搜索它。我的代码已经到了我得到文件的地步,但不确定接下来要使用什么方法。我想要一个类似于 toString() 方法的东西来获取我的 JSON 文件的内容到我的操作中。

我一直在 https://googleapis.dev/nodejs/storage/latest/File.html#get 上搜索,我猜我应该使用 get() 或 createReadStream(),但那里没有太多信息可以帮助指导我哪个更好。

const { conversation } = require('@assistant/conversation');
const functions = require('firebase-functions');
const admin = require('firebase-admin');

const defaultApp = admin.initializeApp({
  credential: admin.credential.applicationDefault()
});
const database = admin.firestore();
var storage = defaultApp.storage();
const bucket = storage.bucket('gs://NAME_OF_BUCKET.appspot.com');
const jsonFile = bucket.file('FILE_NAME.json');

任何帮助将不胜感激,因为我一直在寻找答案一段时间了!谢谢!

【问题讨论】:

标签: javascript firebase firebase-storage


【解决方案1】:

在这种情况下,您需要download()createReadStream(),这对于这种情况是合理的。如果 JSON 文件相对较小(例如 download() 可能更容易编写代码,因为您不需要读取流。 createReadStream() 适用于您可能正在处理单个块中的大文件(例如通过压缩库将其传递到硬盘的过程中)的场景。

const contents = await jsonFile.download();
console.log(contents);
const parsedContents = JSON.parse(contents);

【讨论】:

  • 我用我的文件(大约 355 KB)尝试了这个,但它仍然无法正常工作。它没有从 json 文件中取出任何内容,它只是一个空字符串/json 对象。有什么想法吗?
猜你喜欢
  • 2017-06-18
  • 2017-03-21
  • 2021-05-14
  • 1970-01-01
  • 2023-03-03
  • 1970-01-01
  • 2021-03-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多