简体   繁体   English

尝试通过将 aws 凭据作为变量传递给 airflow 宏来使用 boto3 连接到 aws s3

[英]Trying to connect to aws s3 with boto3 by passing aws credentials as variables to airflow macros

I have one airflow connection that looks like this:我有一个 airflow 连接,如下所示:

  • Conn id: my_conn_id连接 ID: my_conn_id
  • Conn type: s3连接类型: s3
  • Host: my_host主机: my_host
  • Login: abcd登录: abcd

I tried to connect to my s3 using boto3 with the following code without luck我尝试使用以下代码使用 boto3 连接到我的 s3,但没有成功

s3client = boto3.client(
        service_name='s3',
        region_name='us-east-1',
        aws_access_key_id="{{ conn.conn_id.host }}",
        aws_secret_access_key="{{ conn.conn_id.host }}"
    )

Any help would be appreciated任何帮助,将不胜感激

Your connection is misconfigured.您的连接配置错误。 Enter your aws_access_key_id as login and aws_secret_key_id as password.输入您的 aws_access_key_id 作为登录名,输入 aws_secret_key_id 作为密码。 When you generated your AWS keys, you got both the access key and secret key - you will need them both.当您生成 AWS 密钥时,您同时获得了访问密钥和秘密密钥 - 您将同时需要它们。 If you don't have the secret key, regenerate the keys in AWS.如果您没有密钥,请在 AWS 中重新生成密钥。 See connection details here - Airflow AWS connection在此处查看连接详细信息 - Airflow AWS 连接

In Airflow, you should use the S3Hook to generate a boto3 S3 client if you need to, but check out the functionality of the S3Hook first to see if you can use it to do your task.在 Airflow 中,如果需要,您应该使用 S3Hook 生成 boto3 S3 客户端,但请先查看 S3Hook 的功能,看看是否可以使用它来完成任务。 See functions here - S3Hook source code在此处查看函数 - S3Hook 源代码

from airflow.providers.amazon.aws.hooks.s3 import S3Hook
s3client = S3Hook(aws_conn_id=my_conn_id).get_conn()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM