我无法从我的 node.js firebase 函数 index.js 文件中验证大查询

人气:454 发布:2022-10-16 标签: javascript node.js google-bigquery firebase google-cloud-functions

问题描述

我在我的 firebase 函数 index.js 文件中使用大查询 API,但在初始化大查询实例时我不断收到内部服务器错误,我的第一次尝试是在函数目录中包含我的服务帐户密钥文件和我的代码在下面

I am using the big query API in my firebase functions index.js file, but I keep getting an internal server error when initializing the Big query instance, my first attempt was to include my service account key file in the functions directory and my code is below

const functions = require("firebase-functions");
const express = require('express');
const cors = require('cors')({ origin: true });

const app = express();
app.use(cors);
app.use(express.json());



exports.api = functions.https.onRequest(app);


app.post('/create_table', (req, res) => {

  'use strict';

  const { BigQuery } = require('@google-cloud/bigquery');
  const options = {
    keyFilename: 'gamelot-c057c-837d4660ae39.json',
    projectId: 'gamelot-c057c',
  };
  const bigquery = new BigQuery(options);

这是导致错误的行

const { BigQuery } = require('@google-cloud/bigquery');
      const options = {
        keyFilename: 'gamelot-c057c-837d4660ae39.json',
        projectId: 'gamelot-c057c',
      };
      const bigquery = new BigQuery(options);

我该如何解决这个问题?

how can I solve this?

推荐答案

如果您使用 Cloud Functions for Firebase(即通过 Firebase CLI 部署的 Cloud Functions),则在创建 BigQuery 客户端时不应传递任何选项.只需执行以下操作:

If you are using Cloud Functions for Firebase (i.e. Cloud Functions deployed via the Firebase CLI), you should not pass any options when creating the BigQuery client. Just do as follows:

const { BigQuery } = require('@google-cloud/bigquery');
const bigquery = new BigQuery();

事实上,Cloud Functions for Firebase 使用 App Engine 默认服务帐号,即 {project-id}@appspot.gserviceaccount.com,它有一个 Editor 角色,默认包含与 BigQuery 交互的权限.

As a matter of fact, Cloud Functions for Firebase use the App Engine default service account, i.e. {project-id}@appspot.gserviceaccount.com, which has an Editor role, which, by default, contains permissions to interact with BigQuery.

如果编辑器基本",您可以查看此处角色具有所需的权限.

You can check here if the Editor "basic" role has the desired permissions.

296