【问题标题】:GCP service account - access denied for NodeJS scriptGCP 服务帐户 - NodeJS 脚本的访问被拒绝
【发布时间】:2020-06-01 20:50:11
【问题描述】:

你好,我越来越绝望了。

我正在尝试使用 Cron 和 NodeJS 脚本将数据导入 Google BigQuery。但我收到拒绝访问错误:

Access Denied: Dataset arcane-pillar-XXX:MyProj: User does not have bigquery.datasets.get permission for dataset arcane-pillar-XXX:MyProj
  1. 我创建了一个服务帐号 bq-data-import-user@arcane-pillar-XXX.iam.gserviceaccount.com
  2. 我创建了一个密钥,将其下载为 JSON 文件并设置了环境变量
  3. 当我在本地计算机上运行 NodeJS 脚本时出现错误。
  4. 我已经使用 Policy Troubleshooter 测试了该帐户。疑难解答程序说已授予权限。

我多次尝试重新创建帐户,但没有任何帮助。我已经验证没有项目不匹配,因为当我删除数据集时错误会发生变化。

有什么建议吗?

#!/usr/bin/env node
"use strict";

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

process.env["GOOGLE_APPLICATION_CREDENTIALS"] = "/etc/my-app/google-application-credentials.json";


const bq = new BigQuery();
const bqDataset = (await bq.dataset("TheDataset").get({ autoCreate: true }))[0]; // THIS LINE THROWS THE ERROR
const bqTable = (await bqDataset.table("TheTable").get({ autoCreate: true, schema: [ /* The BQ schema */ ] }))[0];

for await (const row of rows) { // Rows is a async generator (cursor in MySQL)
    await bqTable.insert(row);
}

【问题讨论】:

    标签: node.js google-cloud-platform google-bigquery google-iam


    【解决方案1】:

    这是我们的公共article,显示了向 Cloud API 进行身份验证的推荐方式。

    根据我们的公开document,从服务帐户文件在您的应用程序中创建凭据。然后使用凭据通过 BigQuery 客户端库创建服务对象。

    // Create a BigQuery client explicitly using service account credentials.
    // by specifying the private key file.
    
    const {BigQuery} = require('@google-cloud/bigquery');
    
    const options = {
      keyFilename: 'path/to/service_account.json',
      projectId: 'my_project',
    };
    
    const bigquery = new BigQuery(options);
    

    【讨论】:

    • 绝对是一个改进,但我想这不会有帮助。代码实际上按原样工作,但我需要填写服务帐户创建向导的第三步。这解决了我的问题。
    猜你喜欢
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-02
    • 2021-12-01
    • 1970-01-01
    相关资源
    最近更新 更多