-
Notifications
You must be signed in to change notification settings - Fork 17.4k
[AIRFLOW-1520] Boto3 S3Hook, S3Log #2532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,11 +14,64 @@ | |
|
|
||
|
|
||
| import boto3 | ||
| import configparser | ||
|
|
||
| from airflow.exceptions import AirflowException | ||
| from airflow.hooks.base_hook import BaseHook | ||
|
|
||
|
|
||
| def _parse_s3_config(config_file_name, config_format='boto', profile=None): | ||
| """ | ||
| Parses a config file for s3 credentials. Can currently | ||
| parse boto, s3cmd.conf and AWS SDK config formats | ||
|
|
||
| :param config_file_name: path to the config file | ||
| :type config_file_name: str | ||
| :param config_format: config type. One of "boto", "s3cmd" or "aws". | ||
| Defaults to "boto" | ||
| :type config_format: str | ||
| :param profile: profile name in AWS type config file | ||
| :type profile: str | ||
| """ | ||
| Config = configparser.ConfigParser() | ||
| if Config.read(config_file_name): # pragma: no cover | ||
| sections = Config.sections() | ||
| else: | ||
| raise AirflowException("Couldn't read {0}".format(config_file_name)) | ||
| # Setting option names depending on file format | ||
| if config_format is None: | ||
| config_format = 'boto' | ||
| conf_format = config_format.lower() | ||
| if conf_format == 'boto': # pragma: no cover | ||
| if profile is not None and 'profile ' + profile in sections: | ||
| cred_section = 'profile ' + profile | ||
| else: | ||
| cred_section = 'Credentials' | ||
| elif conf_format == 'aws' and profile is not None: | ||
| cred_section = profile | ||
| else: | ||
| cred_section = 'default' | ||
| # Option names | ||
| if conf_format in ('boto', 'aws'): # pragma: no cover | ||
| key_id_option = 'aws_access_key_id' | ||
| secret_key_option = 'aws_secret_access_key' | ||
| # security_token_option = 'aws_security_token' | ||
| else: | ||
| key_id_option = 'access_key' | ||
| secret_key_option = 'secret_key' | ||
| # Actual Parsing | ||
| if cred_section not in sections: | ||
| raise AirflowException("This config file format is not recognized") | ||
| else: | ||
| try: | ||
| access_key = Config.get(cred_section, key_id_option) | ||
| secret_key = Config.get(cred_section, secret_key_option) | ||
| except: | ||
| logging.warning("Option Error in parsing s3 config file") | ||
| raise | ||
| return (access_key, secret_key) | ||
|
|
||
|
|
||
| class AwsHook(BaseHook): | ||
| """ | ||
| Interact with AWS. | ||
|
|
@@ -28,46 +81,59 @@ class AwsHook(BaseHook): | |
| def __init__(self, aws_conn_id='aws_default'): | ||
| self.aws_conn_id = aws_conn_id | ||
|
|
||
| def get_client_type(self, client_type, region_name=None): | ||
| try: | ||
| connection_object = self.get_connection(self.aws_conn_id) | ||
| aws_access_key_id = connection_object.login | ||
| aws_secret_access_key = connection_object.password | ||
| def _get_credentials(self, region_name): | ||
| aws_access_key_id = None | ||
| aws_secret_access_key = None | ||
| s3_endpoint_url = None | ||
|
|
||
| if self.aws_conn_id: | ||
| try: | ||
| connection_object = self.get_connection(self.aws_conn_id) | ||
| if connection_object.login: | ||
| aws_access_key_id = connection_object.login | ||
| aws_secret_access_key = connection_object.password | ||
|
|
||
| elif 'aws_secret_access_key' in connection_object.extra_dejson: | ||
| aws_access_key_id = connection_object.extra_dejson['aws_access_key_id'] | ||
| aws_secret_access_key = connection_object.extra_dejson['aws_secret_access_key'] | ||
|
|
||
| elif 's3_config_file' in connection_object.extra_dejson: | ||
| aws_access_key_id, aws_secret_access_key = \ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please use () for multilines |
||
| _parse_s3_config(connection_object.extra_dejson['s3_config_file'], | ||
| connection_object.extra_dejson.get('s3_config_format')) | ||
|
|
||
| if region_name is None: | ||
| region_name = connection_object.extra_dejson.get('region_name') | ||
|
|
||
| s3_endpoint_url = connection_object.extra_dejson.get('host') | ||
|
|
||
| except AirflowException: | ||
| # No connection found: fallback on boto3 credential strategy | ||
| # http://boto3.readthedocs.io/en/latest/guide/configuration.html | ||
| pass | ||
|
|
||
| if region_name is None: | ||
| region_name = connection_object.extra_dejson.get('region_name') | ||
| return aws_access_key_id, aws_secret_access_key, region_name, s3_endpoint_url | ||
|
|
||
| except AirflowException: | ||
| # No connection found: fallback on boto3 credential strategy | ||
| # http://boto3.readthedocs.io/en/latest/guide/configuration.html | ||
| aws_access_key_id = None | ||
| aws_secret_access_key = None | ||
| def get_client_type(self, client_type, region_name=None): | ||
| aws_access_key_id, aws_secret_access_key, region_name, endpoint_url = \ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. idem |
||
| self._get_credentials(region_name) | ||
|
|
||
| return boto3.client( | ||
| client_type, | ||
| region_name=region_name, | ||
| aws_access_key_id=aws_access_key_id, | ||
| aws_secret_access_key=aws_secret_access_key | ||
| aws_secret_access_key=aws_secret_access_key, | ||
| endpoint_url=endpoint_url | ||
| ) | ||
|
|
||
| def get_resource_type(self, resource_type, region_name=None): | ||
| try: | ||
| connection_object = self.get_connection(self.aws_conn_id) | ||
| aws_access_key_id = connection_object.login | ||
| aws_secret_access_key = connection_object.password | ||
|
|
||
| if region_name is None: | ||
| region_name = connection_object.extra_dejson.get('region_name') | ||
|
|
||
| except AirflowException: | ||
| # No connection found: fallback on boto3 credential strategy | ||
| # http://boto3.readthedocs.io/en/latest/guide/configuration.html | ||
| aws_access_key_id = None | ||
| aws_secret_access_key = None | ||
|
|
||
| aws_access_key_id, aws_secret_access_key, region_name, endpoint_url = \ | ||
| self._get_credentials(region_name) | ||
|
|
||
| return boto3.resource( | ||
| resource_type, | ||
| region_name=region_name, | ||
| aws_access_key_id=aws_access_key_id, | ||
| aws_secret_access_key=aws_secret_access_key | ||
| aws_secret_access_key=aws_secret_access_key, | ||
| endpoint_url=endpoint_url | ||
| ) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is the test to this function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is none, it's legacy code I moved from the S3hook