Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Amazon web services 禁用MWAA气流中的参数验证_Amazon Web Services_Amazon S3_Airflow_Mwaa - Fatal编程技术网

Amazon web services 禁用MWAA气流中的参数验证

Amazon web services 禁用MWAA气流中的参数验证,amazon-web-services,amazon-s3,airflow,mwaa,Amazon Web Services,Amazon S3,Airflow,Mwaa,在MWAA中,我使用以下代码访问S3存储桶中的文件。S3铲斗的形式如下: aws s3 ls s3://example-bucket/incoming/driver-events/ingestDate=2021-05-26/ 上述命令工作正常。现在,我正试图从来自Airflow的S3\u hook.S3Hook()调用中获取相同的信息。我有以下代码: bucket='s3://example-bucket/incoming/driver-events/ingestDate=2021-05-26

在MWAA中,我使用以下代码访问S3存储桶中的文件。S3铲斗的形式如下:

aws s3 ls s3://example-bucket/incoming/driver-events/ingestDate=2021-05-26/
上述命令工作正常。现在,我正试图从来自Airflow的
S3\u hook.S3Hook()
调用中获取相同的信息。我有以下代码:

bucket='s3://example-bucket/incoming/driver-events/ingestDate=2021-05-26/'
s3_handle = S3_hook.S3Hook(aws_conn_id='s3_default')
        key_list = s3_handle.list_keys(bucket_name=bucket)
        print(f"{len(key_list)} keys found in bucket")
        for keys in key_list:
            logging.info(keys)
这导致boto3出现错误:

botocore.exceptions.ParamValidationError: Parameter validation failed:
Invalid bucket name "s3://example-bucket/incoming/driver-events/ingestDate=2021-05-26/": Bucket name must match the regex "^[a-zA-Z0-9.\-_]{1,255}$" or be an ARN matching the regex "^arn:(aws).*:s3:[a-z\-0-9]+:[0-9]{12}:accesspoint[/:][a-zA-Z0-9\-]{1,63}$|^arn:(aws).*:s3-outposts:[a-z\-0-9]+:[0-9]{12}:outpost[/:][a-zA-Z0-9\-]{1,63}[/:]accesspoint[/:][a-zA-Z0-9\-]{1,63}$"

我可以理解错误的出现是因为boto3试图进行一些参数验证,而正则表达式的限制性太强


如何在气流中处理此情况?有没有办法禁用参数验证?我可以看到,通过一些配置设置,可以在boto3中将“参数\u验证”设置为
False
,但是在气流中使用
S3Hook()
时,我该怎么做呢?气流已经以默认方式设置,无法接受boto3配置?更复杂的是,我必须在MWAA上执行此操作,MWAA不能对
~/.boto/
文件夹进行任何控制。

此操作的问题是,bucket应仅为
示例bucket
,路径应为提供给函数调用以列出对象的
前缀。S3不将数据存储为文件夹和文件,而是所有键值对。因此,关键是路径的完整目录结构

工作的代码块如下所示:

bucket_prefix = 'incoming/driver-events/ingestDate=2021-05-26/'
client = boto3.client('s3', 
        aws_access_key_id=Variable.get("AWS_ACCESS_KEY_ID"), 
        aws_secret_access_key=Variable.get("AWS_SECRET_ACCESS_KEY"))
        response = client.list_objects_v2(
            Bucket='example-bucket',
            Delimiter='/',
            Prefix=bucket_prefix,
            MaxKeys=1000,
        )
        print(response)
        contents = response["Contents"]  # These are the files
        common_prefixes = response["CommonPrefixes"] # These are the folders