Taragolis commented on code in PR #34729: URL: https://github.com/apache/airflow/pull/34729#discussion_r1365701214
########## airflow/providers/amazon/aws/fs/s3.py: ########## @@ -0,0 +1,131 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +import logging +from functools import partial +from typing import TYPE_CHECKING, Any, Callable, Dict + +import requests +from botocore import UNSIGNED +from requests import HTTPError + +from airflow.providers.amazon.aws.hooks.base_aws import AwsGenericHook + +if TYPE_CHECKING: + from botocore.awsrequest import AWSRequest + from fsspec import AbstractFileSystem + + +Properties = Dict[str, str] + +S3_PROXY_URI = "s3.proxy-uri" + +log = logging.getLogger(__name__) + +schemes = ["s3", "s3a", "s3n"] + + +class SignError(Exception): + """Raises when unable to sign a S3 request.""" + + +def get_fs(conn_id: str | None) -> AbstractFileSystem: + try: + from s3fs import S3FileSystem + except ImportError: + raise ImportError( + "Airflow FS S3 protocol requires the s3fs library, but it is not installed as it requires" + "aiobotocore. Please install the s3 protocol support library by running: " + "pip install apache-airflow[s3]" + ) + + if conn_id is None: + return S3FileSystem() + + aws: AwsGenericHook = AwsGenericHook(aws_conn_id=conn_id) + + client_kwargs = { + "endpoint_url": aws.conn_config.endpoint_url, + "aws_access_key_id": aws.conn_config.aws_access_key_id, + "aws_secret_access_key": aws.conn_config.aws_secret_access_key, + "aws_session_token": aws.conn_config.aws_session_token, + "region_name": aws.conn_config.region_name, + } Review Comment: There is no guarantee that this fields are non empty in AWS Connection. E.g. this will not work in case of Assume Role ########## airflow/providers/amazon/aws/fs/s3.py: ########## @@ -0,0 +1,131 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +import logging +from functools import partial +from typing import TYPE_CHECKING, Any, Callable, Dict + +import requests +from botocore import UNSIGNED +from requests import HTTPError + +from airflow.providers.amazon.aws.hooks.base_aws import AwsGenericHook + +if TYPE_CHECKING: + from botocore.awsrequest import AWSRequest + from fsspec import AbstractFileSystem + + +Properties = Dict[str, str] + +S3_PROXY_URI = "s3.proxy-uri" + +log = logging.getLogger(__name__) + +schemes = ["s3", "s3a", "s3n"] + + +class SignError(Exception): + """Raises when unable to sign a S3 request.""" + + +def get_fs(conn_id: str | None) -> AbstractFileSystem: + try: + from s3fs import S3FileSystem + except ImportError: + raise ImportError( + "Airflow FS S3 protocol requires the s3fs library, but it is not installed as it requires" + "aiobotocore. Please install the s3 protocol support library by running: " + "pip install apache-airflow[s3]" + ) + + if conn_id is None: + return S3FileSystem() + + aws: AwsGenericHook = AwsGenericHook(aws_conn_id=conn_id) Review Comment: `aws_conn_id=None` is a valid option for [AwsGenericHook](https://airflow.apache.org/docs/apache-airflow-providers-amazon/stable/_api/airflow/providers/amazon/aws/hooks/base_aws/index.html#airflow.providers.amazon.aws.hooks.base_aws.AwsGenericHook) and all childs ########## airflow/providers/amazon/aws/fs/s3.py: ########## @@ -0,0 +1,131 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +import logging +from functools import partial +from typing import TYPE_CHECKING, Any, Callable, Dict + +import requests +from botocore import UNSIGNED +from requests import HTTPError + +from airflow.providers.amazon.aws.hooks.base_aws import AwsGenericHook + +if TYPE_CHECKING: + from botocore.awsrequest import AWSRequest + from fsspec import AbstractFileSystem + + +Properties = Dict[str, str] + +S3_PROXY_URI = "s3.proxy-uri" + +log = logging.getLogger(__name__) + +schemes = ["s3", "s3a", "s3n"] + + +class SignError(Exception): + """Raises when unable to sign a S3 request.""" + + +def get_fs(conn_id: str | None) -> AbstractFileSystem: + try: + from s3fs import S3FileSystem + except ImportError: + raise ImportError( + "Airflow FS S3 protocol requires the s3fs library, but it is not installed as it requires" + "aiobotocore. Please install the s3 protocol support library by running: " + "pip install apache-airflow[s3]" + ) + + if conn_id is None: + return S3FileSystem() + + aws: AwsGenericHook = AwsGenericHook(aws_conn_id=conn_id) + + client_kwargs = { + "endpoint_url": aws.conn_config.endpoint_url, + "aws_access_key_id": aws.conn_config.aws_access_key_id, + "aws_secret_access_key": aws.conn_config.aws_secret_access_key, + "aws_session_token": aws.conn_config.aws_session_token, + "region_name": aws.conn_config.region_name, + } + config_kwargs = {} + register_events: dict[str, Callable[[Properties], None]] = {} + + if signer := aws.conn_config.extra_config.get("s3.signer"): Review Comment: AWS Connection could handle [per service configuration](https://airflow.apache.org/docs/apache-airflow-providers-amazon/stable/connections/aws.html#per-service-configuration), so better move this configurations into the s3 configuration ``` { "service_config": { "s3": { ... } } ``` And this dictionary could be accessed by: https://github.com/apache/airflow/blob/84a3daed8691d5e129eaf3e02061efb8b6ca56cb/airflow/providers/amazon/aws/hooks/base_aws.py#L590-L593 ########## airflow/providers/amazon/aws/fs/s3.py: ########## @@ -0,0 +1,131 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +import logging +from functools import partial +from typing import TYPE_CHECKING, Any, Callable, Dict + +import requests +from botocore import UNSIGNED +from requests import HTTPError + +from airflow.providers.amazon.aws.hooks.base_aws import AwsGenericHook + +if TYPE_CHECKING: + from botocore.awsrequest import AWSRequest + from fsspec import AbstractFileSystem + + +Properties = Dict[str, str] + +S3_PROXY_URI = "s3.proxy-uri" + +log = logging.getLogger(__name__) + +schemes = ["s3", "s3a", "s3n"] + + +class SignError(Exception): + """Raises when unable to sign a S3 request.""" + + +def get_fs(conn_id: str | None) -> AbstractFileSystem: + try: + from s3fs import S3FileSystem + except ImportError: + raise ImportError( + "Airflow FS S3 protocol requires the s3fs library, but it is not installed as it requires" + "aiobotocore. Please install the s3 protocol support library by running: " + "pip install apache-airflow[s3]" + ) + + if conn_id is None: + return S3FileSystem() + + aws: AwsGenericHook = AwsGenericHook(aws_conn_id=conn_id) + + client_kwargs = { + "endpoint_url": aws.conn_config.endpoint_url, + "aws_access_key_id": aws.conn_config.aws_access_key_id, + "aws_secret_access_key": aws.conn_config.aws_secret_access_key, + "aws_session_token": aws.conn_config.aws_session_token, + "region_name": aws.conn_config.region_name, + } + config_kwargs = {} + register_events: dict[str, Callable[[Properties], None]] = {} + + if signer := aws.conn_config.extra_config.get("s3.signer"): + log.info("Loading signer %s", signer) + if singer_func := SIGNERS.get(signer): + uri = aws.conn_config.extra_config.get("s3.signer_uri") + token = aws.conn_config.extra_config.get("s3.signer_token") + if not uri or not token: + raise ValueError(f"Signer {signer} requires uri and token") + + properties: Properties = { + "uri": uri, + "token": uri, + } + singer_func_with_properties = partial(singer_func, properties) + register_events["before-sign.s3"] = singer_func_with_properties + + # Disable the AWS Signer + config_kwargs["signature_version"] = UNSIGNED + else: + raise ValueError(f"Signer not available: {signer}") + + if proxy_uri := aws.conn_config.extra_config.get(S3_PROXY_URI): + config_kwargs["proxies"] = {"http": proxy_uri, "https": proxy_uri} + + fs = S3FileSystem(client_kwargs=client_kwargs, config_kwargs=config_kwargs) Review Comment: I'm not familiar with [S3FileSystem](https://s3fs.readthedocs.io/en/latest/api.html#s3fs.core.S3FileSystem) but could we provide instead of `client_kwargs` aiobotocore session? Which we could be created by `AwsGenericHook.get_session(deferrable=deferrable)` https://github.com/apache/airflow/blob/84a3daed8691d5e129eaf3e02061efb8b6ca56cb/airflow/providers/amazon/aws/hooks/base_aws.py#L610-L614 ########## airflow/providers/amazon/aws/fs/s3.py: ########## @@ -0,0 +1,131 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +import logging +from functools import partial +from typing import TYPE_CHECKING, Any, Callable, Dict + +import requests +from botocore import UNSIGNED +from requests import HTTPError + +from airflow.providers.amazon.aws.hooks.base_aws import AwsGenericHook + +if TYPE_CHECKING: + from botocore.awsrequest import AWSRequest + from fsspec import AbstractFileSystem + + +Properties = Dict[str, str] + +S3_PROXY_URI = "s3.proxy-uri" + +log = logging.getLogger(__name__) + +schemes = ["s3", "s3a", "s3n"] + + +class SignError(Exception): + """Raises when unable to sign a S3 request.""" + + +def get_fs(conn_id: str | None) -> AbstractFileSystem: + try: + from s3fs import S3FileSystem + except ImportError: + raise ImportError( + "Airflow FS S3 protocol requires the s3fs library, but it is not installed as it requires" + "aiobotocore. Please install the s3 protocol support library by running: " + "pip install apache-airflow[s3]" + ) + + if conn_id is None: + return S3FileSystem() + + aws: AwsGenericHook = AwsGenericHook(aws_conn_id=conn_id) + + client_kwargs = { + "endpoint_url": aws.conn_config.endpoint_url, + "aws_access_key_id": aws.conn_config.aws_access_key_id, + "aws_secret_access_key": aws.conn_config.aws_secret_access_key, + "aws_session_token": aws.conn_config.aws_session_token, + "region_name": aws.conn_config.region_name, + } + config_kwargs = {} + register_events: dict[str, Callable[[Properties], None]] = {} + + if signer := aws.conn_config.extra_config.get("s3.signer"): + log.info("Loading signer %s", signer) + if singer_func := SIGNERS.get(signer): + uri = aws.conn_config.extra_config.get("s3.signer_uri") + token = aws.conn_config.extra_config.get("s3.signer_token") + if not uri or not token: + raise ValueError(f"Signer {signer} requires uri and token") + + properties: Properties = { + "uri": uri, + "token": uri, + } + singer_func_with_properties = partial(singer_func, properties) + register_events["before-sign.s3"] = singer_func_with_properties + + # Disable the AWS Signer + config_kwargs["signature_version"] = UNSIGNED + else: + raise ValueError(f"Signer not available: {signer}") + + if proxy_uri := aws.conn_config.extra_config.get(S3_PROXY_URI): + config_kwargs["proxies"] = {"http": proxy_uri, "https": proxy_uri} + + fs = S3FileSystem(client_kwargs=client_kwargs, config_kwargs=config_kwargs) Review Comment: `config_kwargs` looks the similar as `config_kwargs` from [AWS Connection Extras](https://airflow.apache.org/docs/apache-airflow-providers-amazon/stable/connections/aws.html#configuring-the-connection) ########## airflow/providers/amazon/aws/fs/s3.py: ########## @@ -0,0 +1,131 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +import logging +from functools import partial +from typing import TYPE_CHECKING, Any, Callable, Dict + +import requests +from botocore import UNSIGNED +from requests import HTTPError + +from airflow.providers.amazon.aws.hooks.base_aws import AwsGenericHook + +if TYPE_CHECKING: + from botocore.awsrequest import AWSRequest + from fsspec import AbstractFileSystem + + +Properties = Dict[str, str] + +S3_PROXY_URI = "s3.proxy-uri" + +log = logging.getLogger(__name__) + +schemes = ["s3", "s3a", "s3n"] + + +class SignError(Exception): + """Raises when unable to sign a S3 request.""" + + +def get_fs(conn_id: str | None) -> AbstractFileSystem: + try: + from s3fs import S3FileSystem + except ImportError: + raise ImportError( + "Airflow FS S3 protocol requires the s3fs library, but it is not installed as it requires" + "aiobotocore. Please install the s3 protocol support library by running: " + "pip install apache-airflow[s3]" + ) + + if conn_id is None: + return S3FileSystem() + + aws: AwsGenericHook = AwsGenericHook(aws_conn_id=conn_id) Review Comment: ```suggestion aws = AwsGenericHook(aws_conn_id=conn_id, client_type="s3") ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
