-
Notifications
You must be signed in to change notification settings - Fork 16.3k
Add support for S3 dag bundle #46621
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
abf8a88
Implement S3 dag bundle
ismailsimsek faed04e
Update providers/amazon/src/airflow/providers/amazon/aws/bundles/s3.py
ismailsimsek 7004538
remove empty arg
ismailsimsek 3a5892f
test empty prefix
ismailsimsek 2bb8633
change logging
ismailsimsek 87cc522
Update providers/amazon/src/airflow/providers/amazon/aws/bundles/s3.py
ismailsimsek 8450d3f
Update providers/amazon/src/airflow/providers/amazon/aws/bundles/s3.py
ismailsimsek 3f222b1
Add review changes
ismailsimsek c0d018e
Add review changes
ismailsimsek 5eeeee1
Add review changes
ismailsimsek d0c1a17
fix linting
ismailsimsek f134a6b
review change, improve naming
ismailsimsek 340d251
review change, add test to hook
ismailsimsek 0ab61de
review change, add test to hook
ismailsimsek 23532bf
review change, add test to hook
ismailsimsek 02505bb
Fixes/Updates to get PR passing
o-nikolas 9fd5298
Fixes/Updates to get PR passing
o-nikolas 17eb628
Fixes/Updates to get PR passing
o-nikolas 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
16 changes: 16 additions & 0 deletions
16
providers/amazon/src/airflow/providers/amazon/aws/bundles/__init__.py
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 |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # 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. |
152 changes: 152 additions & 0 deletions
152
providers/amazon/src/airflow/providers/amazon/aws/bundles/s3.py
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 |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| # 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 os | ||
| from pathlib import Path | ||
|
|
||
| import structlog | ||
|
|
||
| from airflow.dag_processing.bundles.base import BaseDagBundle | ||
| from airflow.exceptions import AirflowException | ||
| from airflow.providers.amazon.aws.hooks.base_aws import AwsBaseHook | ||
| from airflow.providers.amazon.aws.hooks.s3 import S3Hook | ||
|
|
||
|
|
||
| class S3DagBundle(BaseDagBundle): | ||
| """ | ||
| S3 DAG bundle - exposes a directory in S3 as a DAG bundle. | ||
|
|
||
| This allows Airflow to load DAGs directly from an S3 bucket. | ||
|
|
||
| :param aws_conn_id: Airflow connection ID for AWS. Defaults to AwsBaseHook.default_conn_name. | ||
| :param bucket_name: The name of the S3 bucket containing the DAG files. | ||
| :param prefix: Optional subdirectory within the S3 bucket where the DAGs are stored. | ||
| If None, DAGs are assumed to be at the root of the bucket (Optional). | ||
| """ | ||
|
|
||
| supports_versioning = False | ||
|
|
||
| def __init__( | ||
| self, | ||
| *, | ||
| aws_conn_id: str = AwsBaseHook.default_conn_name, | ||
| bucket_name: str, | ||
| prefix: str = "", | ||
| **kwargs, | ||
| ) -> None: | ||
| super().__init__(**kwargs) | ||
| self.aws_conn_id = aws_conn_id | ||
| self.bucket_name = bucket_name | ||
| self.prefix = prefix | ||
| # Local path where S3 DAGs are downloaded | ||
| self.s3_dags_dir: Path = self.base_dir | ||
|
|
||
| log = structlog.get_logger(__name__) | ||
| self._log = log.bind( | ||
| bundle_name=self.name, | ||
| version=self.version, | ||
| bucket_name=self.bucket_name, | ||
| prefix=self.prefix, | ||
| aws_conn_id=self.aws_conn_id, | ||
| ) | ||
| self._s3_hook: S3Hook | None = None | ||
|
|
||
| def _initialize(self): | ||
| with self.lock(): | ||
| if not self.s3_dags_dir.exists(): | ||
| self._log.info("Creating local DAGs directory: %s", self.s3_dags_dir) | ||
| os.makedirs(self.s3_dags_dir) | ||
|
|
||
| if not self.s3_dags_dir.is_dir(): | ||
| raise AirflowException(f"Local DAGs path: {self.s3_dags_dir} is not a directory.") | ||
|
|
||
| if not self.s3_hook.check_for_bucket(bucket_name=self.bucket_name): | ||
| raise AirflowException(f"S3 bucket '{self.bucket_name}' does not exist.") | ||
|
|
||
| if self.prefix: | ||
| # don't check when prefix is "" | ||
| if not self.s3_hook.check_for_prefix( | ||
| bucket_name=self.bucket_name, prefix=self.prefix, delimiter="/" | ||
| ): | ||
| raise AirflowException( | ||
| f"S3 prefix 's3://{self.bucket_name}/{self.prefix}' does not exist." | ||
| ) | ||
| self.refresh() | ||
|
|
||
| def initialize(self) -> None: | ||
| self._initialize() | ||
| super().initialize() | ||
|
|
||
| @property | ||
| def s3_hook(self): | ||
| if self._s3_hook is None: | ||
| try: | ||
| self._s3_hook: S3Hook = S3Hook(aws_conn_id=self.aws_conn_id) # Initialize S3 hook. | ||
| except AirflowException as e: | ||
| self._log.warning("Could not create S3Hook for connection %s: %s", self.aws_conn_id, e) | ||
| return self._s3_hook | ||
|
|
||
| def __repr__(self): | ||
| return ( | ||
| f"<S3DagBundle(" | ||
| f"name={self.name!r}, " | ||
| f"bucket_name={self.bucket_name!r}, " | ||
| f"prefix={self.prefix!r}, " | ||
| f"version={self.version!r}" | ||
| f")>" | ||
| ) | ||
|
|
||
| def get_current_version(self) -> str | None: | ||
| """Return the current version of the DAG bundle. Currently not supported.""" | ||
| return None | ||
|
|
||
| @property | ||
| def path(self) -> Path: | ||
| """Return the local path to the DAG files.""" | ||
| return self.s3_dags_dir # Path where DAGs are downloaded. | ||
|
|
||
| def refresh(self) -> None: | ||
| """Refresh the DAG bundle by re-downloading the DAGs from S3.""" | ||
| if self.version: | ||
| raise AirflowException("Refreshing a specific version is not supported") | ||
|
|
||
| with self.lock(): | ||
| self._log.debug( | ||
| "Downloading DAGs from s3://%s/%s to %s", self.bucket_name, self.prefix, self.s3_dags_dir | ||
| ) | ||
| self.s3_hook.sync_to_local_dir( | ||
| bucket_name=self.bucket_name, | ||
| s3_prefix=self.prefix, | ||
| local_dir=self.s3_dags_dir, | ||
| delete_stale=True, | ||
| ) | ||
|
|
||
| def view_url(self, version: str | None = None) -> str | None: | ||
| """Return a URL for viewing the DAGs in S3. Currently, versioning is not supported.""" | ||
| if self.version: | ||
| raise AirflowException("S3 url with version is not supported") | ||
|
|
||
| # https://<bucket-name>.s3.<region>.amazonaws.com/<object-key> | ||
| url = f"https://{self.bucket_name}.s3" | ||
| if self.s3_hook.region_name: | ||
| url += f".{self.s3_hook.region_name}" | ||
| url += ".amazonaws.com" | ||
| if self.prefix: | ||
| url += f"/{self.prefix}" | ||
|
|
||
| return url | ||
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
16 changes: 16 additions & 0 deletions
16
providers/amazon/tests/unit/amazon/aws/bundles/__init__.py
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 |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # 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. |
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.
Uh oh!
There was an error while loading. Please reload this page.