使用 Google Actions 进行 Firebase 存储 [英] Firebase Storage with Google Actions

查看:19
本文介绍了使用 Google Actions 进行 Firebase 存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将我的 Firebase 存储与我的 google 操作连接起来时遇到了一些问题.我需要能够下载"json 文件,以便能够读取和挑选用户在调用操作时提供的数据可能需要的内容.

I am having some issues connecting my firebase storage with my google action. I need to be able to "download" the json files inside in order to be able to read and pick out what a user may need given data that they provide when they call the action.

以下是我目前拥有的代码,是根据我发现的不同 API 和其他计算器问题编写的.

Below is the code that I currently have, complied from the different APIs and other stackoverflow questions I have found.

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const Firestore = require('@google-cloud/firestore');
const firestore = new Firestore();

var storage = require('@google-cloud/storage');
const gcs = storage({projectId: 'aur-healthcare-group'});
const bucket = gcs.bucket('gs://aur-healthcare-group');


admin.storage().bucket().file('aur-healthcare-group/aur_members.json').download(function(errr, contents){
  if(!err){
    var jsObjext = JSON.parse(contents.toString('utf8'));
  }
});

我收到的当前错误是code":3,message":Function failed on loading user code.这可能是由于用户代码中的错误.错误消息:错误:请检查您的函数日志以查看错误原因.当我检查日志时,我只再次收到上述消息.

The current error I am receiving is "code":3,"message":"Function failed on loading user code. This is likely due to a bug in the user code. Error message: Error: please examine your function logs to see the error cause. When I check the logs I only get the above mentioned message again.

我相信我没有正确访问我的 firebase 存储,并且无法找到有关如何正确访问它的好资源.有人能给我一个如何正确访问存储的示例,以便我能够将其应用到我的项目中吗?

I believe that I am not accessing my firebase storage correctly and have trouble finding a good resource on how to access this correctly. Would somebody be able to give me an example of how to access the storage correctly so I will be able to apply it to my project?

推荐答案

由于您在 Firebase Functions 中运行,您不需要直接要求 @google-cloud/storage 依赖项.相反,您可以通过 admin.storage()

Since you're running in Firebase Functions, you shouldn't need to require the @google-cloud/storage dependency directly. Rather, you can get the correctly authenticated storage component via admin.storage()

在此之后,您不应将文件下载到您的函数中,因为您最好通过 readStream 直接读入内存.

Following that, you shouldn't download the file to your function, as you would be better off reading directly into memory via a readStream.

关于您现有的代码错误,可能是因为您正在检查if (!err),而回调变量为errr.

With regards to your existing code error, it may be because you're checking if (!err) when the callback variable is errr.

我过去曾这样做过,下面是我如何实现它的代码片段.它是专门用 Typescript 编写的,但我认为如果你直接使用它,你应该能够将它移植到 JS.

I've done this in the past and here's a code snippet of how I achieved it. It's written in Typescript specifically, but I think you should be able to port it to JS if you're using that directly.

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin'
import { Bucket } from '@google-cloud/storage';

admin.initializeApp()

const db = admin.firestore()

const bucket = admin.storage().bucket('project-id.appspot.com') // Use your project-id here.

const readFile = async (bucket: Bucket, fileName: string) => {
  const stream = bucket.file(fileName).createReadStream();
  return new Promise((resolve, reject) => {
    let buffer = '';
    stream.on('data', function(d: string) {
      buffer += d;
    }).on('end', function() {
      resolve(buffer)
    });
  })
}

app.handle('my-intent-handler', async (conv) => {
  const contents = await readArticle(bucket, 'filename.txt')
  conv.add(`Your content is ${contents}`)
})

exports.fulfillment = functions.https.onRequest(app)

这篇关于使用 Google Actions 进行 Firebase 存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆