添加与 gdrive 范围的 BigQuery 连接?
Adding BigQuery connection with gdrive scopes?
我有一个外部表格 table,我想通过 Airflow 中的 BigQueryOperator 进行查询。
我更愿意使用 Cloud Composer 服务帐户。
我使用以下参数通过 Airflow UI 创建了一个新连接:
Conn Id: bigquery_with_gdrive_scope
Conn Type: google_cloud_platform
Project Id: <my project id>
Keyfile path: <none>
Keyfile JSON: <none>
Scopes: https://www.googleapis.com/auth/bigquery,https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/drive
在我的 DAG 中,我使用:BigQueryOperator(..., bigquery_conn_id='bigquery_with_gdrive_scope')
日志报告:Access Denied: BigQuery BigQuery: No OAuth token with Google Drive scope was found.
任务属性显示:bigquery_conn_id bigquery_with_gdrive_scope
就好像 bigquery_conn_id
参数被忽略了。
万一有人遇到同样的问题,(Composer 1.0.0,Airflow 1.9.0)回退到 gcloud auth
,除非提供 Keyfile path
或 Keyfile json
。这将忽略任何范围参数。
Airflow 的 master 分支修复了这个问题;但现在您必须为服务帐户生成一个凭据文件,并告诉 Airflow 这些文件位于何处。
有分步说明here。
对于我的用例,我为 airflow 的服务帐户创建了一个密钥并设置了如下连接:
Conn Id: bigquery_with_gdrive_scope
Conn Type: google_cloud_platform
Project Id: <my project id>
Keyfile path: <none>
Keyfile JSON: <contents of keyfile for airflow service account>
Scopes: https://www.googleapis.com/auth/bigquery,https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/drive
添加 GCP API 范围(如接受的答案)对我们不起作用。经过大量调试,GCP 似乎有 "root" 个在创建期间分配给环境的范围,并且无法通过 Airflow Connections 覆盖。这似乎只影响 GCP API 范围。
作为参考,我们使用了 composer 1.4.0
和 airflow 1.10.0
如果您想在 Cloud Composer 上添加与 GCP 有关的范围,则必须在 create the environment 时这样做。事后无法修改。
创建环境时,请务必添加 https://www.googleapis.com/auth/drive
。具体来说,您可以将以下标志添加到 gcloud composer environment create
命令中:
--oauth-scopes=https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/drive
最后,不要忘记与服务帐户电子邮件共享文档(除非您已授予服务帐户域范围访问权限)
我有一个外部表格 table,我想通过 Airflow 中的 BigQueryOperator 进行查询。
我更愿意使用 Cloud Composer 服务帐户。
我使用以下参数通过 Airflow UI 创建了一个新连接:
Conn Id: bigquery_with_gdrive_scope
Conn Type: google_cloud_platform
Project Id: <my project id>
Keyfile path: <none>
Keyfile JSON: <none>
Scopes: https://www.googleapis.com/auth/bigquery,https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/drive
在我的 DAG 中,我使用:BigQueryOperator(..., bigquery_conn_id='bigquery_with_gdrive_scope')
日志报告:Access Denied: BigQuery BigQuery: No OAuth token with Google Drive scope was found.
任务属性显示:bigquery_conn_id bigquery_with_gdrive_scope
就好像 bigquery_conn_id
参数被忽略了。
万一有人遇到同样的问题,(Composer 1.0.0,Airflow 1.9.0)回退到 gcloud auth
,除非提供 Keyfile path
或 Keyfile json
。这将忽略任何范围参数。
Airflow 的 master 分支修复了这个问题;但现在您必须为服务帐户生成一个凭据文件,并告诉 Airflow 这些文件位于何处。
有分步说明here。
对于我的用例,我为 airflow 的服务帐户创建了一个密钥并设置了如下连接:
Conn Id: bigquery_with_gdrive_scope
Conn Type: google_cloud_platform
Project Id: <my project id>
Keyfile path: <none>
Keyfile JSON: <contents of keyfile for airflow service account>
Scopes: https://www.googleapis.com/auth/bigquery,https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/drive
添加 GCP API 范围(如接受的答案)对我们不起作用。经过大量调试,GCP 似乎有 "root" 个在创建期间分配给环境的范围,并且无法通过 Airflow Connections 覆盖。这似乎只影响 GCP API 范围。
作为参考,我们使用了 composer 1.4.0
和 airflow 1.10.0
如果您想在 Cloud Composer 上添加与 GCP 有关的范围,则必须在 create the environment 时这样做。事后无法修改。
创建环境时,请务必添加 https://www.googleapis.com/auth/drive
。具体来说,您可以将以下标志添加到 gcloud composer environment create
命令中:
--oauth-scopes=https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/drive
最后,不要忘记与服务帐户电子邮件共享文档(除非您已授予服务帐户域范围访问权限)