1 回答
TA贡献1820条经验 获得超9个赞
正如您提到的,GCP 目前使用服务帐户、凭据 JSON 和 API 令牌进行身份验证。您可以考虑使用 AWS Secrets Manager 或 AWS Systems Manager Parameter Store 来存储 GCP 凭证,然后在 Jupyter notebook 中获取它们,而不是将凭证存储在 S3 存储桶中。通过这种方式可以保护凭证,并且仅在需要时才从 Secrets Manager 创建凭证文件。
这是我之前用于从 SageMaker 实例连接到 BigQuery 的示例代码。
import os
import json
import boto3
from google.cloud.bigquery import magics
from google.oauth2 import service_account
def get_gcp_credentials_from_ssm(param_name):
# read credentials from SSM parameter store
ssm = boto3.client('ssm')
# Get the requested parameter
response = ssm.get_parameters(Names=[param_name], WithDecryption=True)
# Store the credentials in a variable
gcp_credentials = response['Parameters'][0]['Value']
# save credentials temporarily to a file
credentials_file = '/tmp/.gcp/service_credentials.json'
with open(credentials_file, 'w') as outfile:
json.dump(json.loads(gcp_credentials), outfile)
# create google.auth.credentials.Credentials to use for queries
credentials = service_account.Credentials.from_service_account_file(credentials_file)
# remove temporary file
if os.path.exists(credentials_file):
os.remove(credentials_file)
return credentials
# this will set the context credentials to use for queries performed in jupyter
# using bigquery cell magic
magics.context.credentials = get_gcp_credentials_from_ssm('my_gcp_credentials')
请注意,SageMaker 执行角色应该有权访问 SSM,当然还有其他必要的路径来连接到 GCP。我不确定这是否是最好的方法。希望有人有更好的方法。
添加回答
举报