1

我写了一些代码,使用服务帐户在 google-cloud 上写入 BQ。

一件很奇怪的事情是,只有使用 DML 的“更新”操作才会失败。(其他插入、删除RPC调用成功)。

    def create_table(self, table_id, schema):
        table_full_name = self.get_table_full_name(table_id)
        table = self.get_table(table_full_name)
        if table is not None:
            return  # self.client.delete_table(table_full_name, not_found_ok=True)  # Make an API
            # request.  # print("Deleted table '{}'.".format(table_full_name))
        table = bigquery.Table(table_full_name, schema=schema)
        table = self.client.create_table(table)  # Make an API request.
        print("Created table {}.{}.{}".format(table.project, table.dataset_id, table.table_id))


#Works!
    def upload_rows_to_bq(self, table_id, rows_to_insert):
        table_full_name = self.get_table_full_name(table_id)
        for ads_chunk in split(rows_to_insert, _BQ_CHUNK_SIZE):
            errors = self.client.insert_rows_json(table_full_name, ads_chunk,
                row_ids=[None] * len(rows_to_insert))  # Make an API request.
            if not errors:
                print("New rows have been added.")
            else:
                print("Encountered errors while inserting rows: {}".format(errors))

#Permissions Failure
    def update_bq_ads_status_removed(self, table_id, update_ads):
        affected_rows = 0
        table_full_name = self.get_table_full_name(table_id)
        for update_ads_chunk in split(update_ads, _BQ_CHUNK_SIZE):
            ad_ids = [item["ad_id"] for item in update_ads_chunk]
            affected_rows += self.update_bq_ads_status(f"""
                            UPDATE {table_full_name} 
                            SET status = 'Removed' 
                            WHERE ad_id IN {tuple(ad_ids)} 
                            """)
        return affected_rows

我收到此错误仅用于更新:

User does not have bigquery.jobs.create permission in project ABC.

4

2 回答 2

0

我将详细说明我的评论。

在 GCP 中,您有 3 种类型的 IAM 角色。

包括所有者、编辑者和查看者角色。

为特定服务提供精细访问权限,并由 Google Cloud 管理。预定义角色旨在支持常见用例和访问控制模式。

根据用户指定的权限列表提供精细访问。

预定义角色和自定义角色有什么区别?如果您更改(添加/删除)predefinied角色的权限,它将变为custom role.

可以在此处找到具有权限列表的 BigQuery 的预定义角色

提到的错误:

用户在项目 ABC 中没有 bigquery.jobs.create 权限。

表示IAM Role没有特定BigQuery 权限- bigquery.jobs.create

bigquery.jobs.create权限可以在两个预定义的角色中找到,例如:

  • BigQuery 作业用户 - (roles/bigquery.jobUser)
  • BigQuery 用户 - (roles/bigquery.user)

或者可以添加到不同的predefinied role,但它会更改为custom role.

另外,在测试权限指南中,您可以找到有关如何测试 IAM 权限的信息。

于 2021-09-27T16:32:18.507 回答
-3

请为服务帐户指定bigquery.user角色并尝试再次运行代码。

BigQuery Job User
role: bigquery.user
于 2021-09-23T21:29:30.653 回答