【问题标题】:Google Cloud Function Accessing BigQuery谷歌云函数访问 BigQuery
【发布时间】:2018-03-01 23:08:04
【问题描述】:

我正在尝试编写一个访问我的 bigquery 数据的谷歌云函数。但是,当我运行它时出现错误。如果我在本地模拟器下运行它可以正常工作,但在部署时就不行。

我的 package.json:

{
  "dependencies": {
    "@google-cloud/bigquery": "^0.9.6"
  }
}

index.js

var BigQuery = require('@google-cloud/bigquery');

exports.hello = function hello(req, res) {

    var bigQuery = BigQuery({ projectId: 'influence-ranking' });
    bigQuery.query({
        query: 'SELECT label from `influence-ranking.wikidata.labels` LIMIT 1',
        useLegacySql: false
    }).then(function () {
        return res.status(200).send('hello');
    }).catch(function (error) {
        return res.status(500).json(error);
    });
};

我部署函数:

$ gcloud beta functions deploy hello --stage-bucket influence-ranking-web --trigger-http --project influence-ranking

Copying file:///tmp/tmpwR3Sza/fun.zip [Content-Type=application/zip]...
/ [1 files][  7.3 KiB/  7.3 KiB]                                                
Operation completed over 1 objects/7.3 KiB.                                      
Deploying function (may take a while - up to 2 minutes)...done.                                                                      
availableMemoryMb: 256
entryPoint: hello
httpsTrigger:
  url: https://us-central1-influence-ranking.cloudfunctions.net/hello
labels:
  deployment-tool: cli-gcloud
latestOperation: operations/aW5mbHVlbmNlLXJhbmtpbmcvdXMtY2VudHJhbDEvaGVsbG8vdHJoV1pjeWlnSkk
name: projects/influence-ranking/locations/us-central1/functions/hello
serviceAccount: tfc7c97f9c5ac6052-tp@appspot.gserviceaccount.com
sourceArchiveUrl: gs://influence-ranking-web/us-central1-hello-poucdhlbhptc.zip
status: READY
timeout: 60s
updateTime: '2017-09-20T18:42:22Z'
versionId: '15'


$ curl https://us-central1-influence-ranking.cloudfunctions.net/hello
{"code":403,"errors":[{"domain":"usageLimits","reason":"accessNotConfigured","message":"Access Not Configured. BigQuery API has not been used in project 283683622993 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/bigquery.googleapis.com/overview?project=283683622993 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.","extendedHelp":"https://console.developers.google.com/apis/api/bigquery.googleapis.com/overview?project=283683622993"}],"message":"Access Not Configured. BigQuery API has not been used in project 283683622993 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/bigquery.googleapis.com/overview?project=283683622993 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry."}

错误信息中的链接给出了错误:

The API "bigquery.googleapis.com" doesn't exist or you don't have permission to access it

项目参数似乎与我在谷歌云控制台中为我的项目列出的项目编号不对应。所以这让我觉得我以某种方式收到了针对错误项目的请求,但我不知道如何。

【问题讨论】:

    标签: google-cloud-platform google-cloud-functions


    【解决方案1】:

    我的谷歌云功能帐户出了点问题。

    我结束了:

    1. 删除我的所有函数
    2. 禁用谷歌云功能
    3. 重新启用谷歌云功能

    现在它可以工作了。

    重要的是,现在部署的输出显示:

    serviceAccount: influence-ranking@appspot.gserviceaccount.com
    

    所以它使用了正确的服务帐号。

    【讨论】:

    • 您不必手动配置服务帐户,对吧?我试图弄清楚 Cloud Functions 和 BigQuery 如何在不设置服务帐户的情况下协同工作。似乎谷歌会自动执行此操作,只要 2 件事在同一个 GCP 项目下
    • @HoangTrinh,抱歉无法详细回忆两年前这是如何工作的。
    猜你喜欢
    • 1970-01-01
    • 2020-02-01
    • 2017-08-20
    • 2014-10-30
    • 2018-07-25
    • 2023-04-06
    • 2019-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多