Gabriel39 commented on a change in pull request #17201: URL: https://github.com/apache/airflow/pull/17201#discussion_r677443872
########## File path: airflow/providers/alibabacloud/hooks/oss.py ########## @@ -0,0 +1,441 @@ +# +# 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 functools import wraps +from inspect import signature +import oss2 +from oss2.exceptions import ClientError +from typing import Callable, TYPE_CHECKING, TypeVar, cast, Optional +from urllib.parse import urlparse + +from airflow.exceptions import AirflowException +from airflow.hooks.base import BaseHook + + +if TYPE_CHECKING: + from airflow.models.connection import Connection + +T = TypeVar("T", bound=Callable) + + +def provide_bucket_name(func: T) -> T: + """ + Function decorator that unifies bucket name and key taken from the key + in case no bucket name and at least a key has been passed to the function. + """ + function_signature = signature(func) + + @wraps(func) + def wrapper(*args, **kwargs) -> T: + bound_args = function_signature.bind(*args, **kwargs) + self = args[0] + + def get_credential(oss_conn, arguments) -> oss2.auth.Auth: + extra_config = oss_conn.extra_dejson + auth_type = extra_config.get('auth_type', None) + if not auth_type: + raise Exception("No auth_type specified in extra_config, either 'AK' or 'STS'") + + if auth_type == 'AK': + oss_access_key_id = arguments['access_id'] \ + if 'access_id' in bound_args.arguments and bound_args.arguments['access_id'] is not None \ + else extra_config.get('access_key_id', None) + oss_access_key_secret = arguments['access_secret'] \ + if 'access_secret' in bound_args.arguments and bound_args.arguments['access_secret'] is not None \ + else extra_config.get('access_key_secret', None) + if not oss_access_key_id: + raise Exception("No access_key_id is specified for connection: " + self.conn_id) + if not oss_access_key_secret: + raise Exception("No access_key_secret is specified for connection: " + self.conn_id) + return oss2.Auth(oss_access_key_id, oss_access_key_secret) + else: + raise Exception("Unsupported auth_type: " + auth_type) + + if 'auth' not in bound_args.arguments or bound_args.arguments['auth'] is None: + bound_args.arguments['auth'] = get_credential(self.oss_conn, bound_args.arguments) + + if 'bucket_name' not in bound_args.arguments or bound_args.arguments['bucket_name'] is None: + if self.oss_conn_id: + connection = self.get_connection(self.oss_conn_id) + if connection.schema: + bound_args.arguments['bucket_name'] = connection.schema Review comment: done -- 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]
