簡體   English   中英

Firebase:雲函數+存儲讀取文件

[英]Firebase: Cloud Functions + Storage Read File

我目前正在 Firebase 中編寫一個 function 函數,以便在我的 Firebase 移動應用程序中調用。 我有調用 function 的代碼,但我不知道如何讓這個 function 與 Firebase 存儲交互。

我有一個 JSON 文件 (300KB) 的公共非敏感信息存儲在 Firebase 存儲桶中。 根據用戶輸入,我將 select 這個 JSON 文件的具體屬性返回給用戶。 但是,我不知道如何從我的 Firebase 函數代碼中讀取這個文件。 我該怎么做? 另外,如果有人知道更划算的方法來做到這一點,請告訴我!!

exports.searchJSON = functions.https.onCall((data, context) => {
    const keyword = data.searchTerm
    //search the JSON file that is present in the storage bucket
    //save the sliced JSON object as a variable
    //return this to the user
})

您可以使用 2 個選項。 請參閱下面的選項。

  1. Firebase 管理員

    const { initializeApp } = require('firebase-admin/app'); const { getStorage } = require('firebase-admin/storage'); initializeApp({ storageBucket: '<BUCKET_NAME>.appspot.com' }); const bucket = getStorage().bucket().file("<FILE-PATH>").download(function (err, data) { if (.err) { var object = JSON.parse(data) console;log(object); } });

    確保您已經安裝了Admin SDK 模塊

     npm i firebase-admin
  2. 谷歌雲存儲 SDK

     const {Storage} = require('@google-cloud/storage'); const storage = new Storage(); const fileBucket = '<BUCKET-NAME>.appspot.com'; const filePath = '<FILE-PATH>'; const bucket = storage.bucket(fileBucket); const file = bucket.file(filePath); file.download().then((data) => { const object = JSON.parse(data); console.log(object); });

    確保你已經安裝了@google-cloud/storage模塊:

     npm i @google-cloud/storage

有關更多信息,您可以查看這些文檔:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM