是的,BigQuery数据传输服务支持使用临时的AWS IAM凭证,以便可以在BigQuery中访问AWS存储桶中的数据。
以下是一个Python示例,说明如何使用AWS临时凭证来传输数据:
from google.cloud import bigquery_datatransfer_v1
client = bigquery_datatransfer_v1.DataTransferServiceClient()
# 将AWS访问密钥ID和访问密钥秘密检索到环境变量中
aws_access_key_id = os.environ["AWS_ACCESS_KEY_ID"]
aws_secret_access_key = os.environ["AWS_SECRET_ACCESS_KEY"]
# 创建AWS临时凭证对象
sts_client = boto3.client('sts', aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key)
response = sts_client.assume_role(RoleArn='arn:aws:iam::123456789012:role/MyRole', RoleSessionName='MySession')
# 使用AWS临时凭证创建数据传输配置
parent = client.project_path('my-project-id')
transfer_config = {
"destination_dataset_id": "my_destination_dataset",
"display_name": "My Transfer Configuration",
"data_source_id": "amazon_s3",
"params": {
"aws_access_key_id": response['Credentials']['AccessKeyId'],
"aws_secret_access_key": response['Credentials']['SecretAccessKey'],
"aws_session_token": response['Credentials']['SessionToken'],
"bucket_name": "my-s3-bucket",
"prefix": "my-folder/",
"file_format": "CSV",
"compression": "NONE"
}
}
response = client.create_transfer_config(parent=parent, transfer_config=transfer_config)
# 启动数据传输作业
transfer_job = {
"project_id": "my-project-id",
"transfer_config": response.name,
"schedule_config": {
"schedule_start_time": {"seconds": int(time.time() + 3600)},
"schedule_end_time": {"seconds": int(time.time() + 60 * 60 * 24 * 365)},