Nia*_*dal 1 node.js google-cloud-storage firebase google-cloud-platform google-cloud-functions
我目前正在 Firebase Functions 中编写一个函数,以便在我的 Firebase 移动应用程序中调用。我有调用该函数的代码,但我不知道如何让该函数与 Firebase 存储交互。
我有一个存储在 Firebase 存储桶中的公共非敏感信息的 JSON 文件 (300KB)。根据用户输入,我将选择此 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
})
Run Code Online (Sandbox Code Playgroud)
您有 2 个选项可以使用。请参阅下面的选项。
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);
}
});
Run Code Online (Sandbox Code Playgroud)
确保您已安装Admin SDK 模块。
npm i firebase-admin
Run Code Online (Sandbox Code Playgroud)
谷歌云存储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);
});
Run Code Online (Sandbox Code Playgroud)
确保您已安装该@google-cloud/storage模块:
npm i @google-cloud/storage
Run Code Online (Sandbox Code Playgroud)
欲了解更多信息,您可以查看这些文档:
| 归档时间: |
|
| 查看次数: |
2431 次 |
| 最近记录: |