generated from MITLibraries/python-cli-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Add option to download input files using a local MinIO server #49
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
2 commits
Select commit
Hold shift + click to select a range
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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
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
Empty file.
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,17 @@ | ||
| services: | ||
| minio: | ||
| image: quay.io/minio/minio:latest | ||
| command: server --console-address ":9001" /mnt/data | ||
| ports: | ||
| - "9000:9000" # API port | ||
| - "9001:9001" # Console port | ||
| environment: | ||
| MINIO_ROOT_USER: ${MINIO_ROOT_USER} | ||
| MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD} | ||
| healthcheck: | ||
| test: ["CMD", "mc", "ready", "local"] | ||
| interval: 5s | ||
| timeout: 5s | ||
| retries: 5 | ||
| volumes: | ||
| - ${MINIO_S3_LOCAL_STORAGE}:/mnt/data |
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,101 @@ | ||
| import logging | ||
| import subprocess | ||
|
|
||
| import boto3 | ||
| from botocore.exceptions import ClientError | ||
| from mypy_boto3_s3.client import S3Client | ||
|
|
||
| from abdiff.config import Config | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| CONFIG = Config() | ||
|
|
||
|
|
||
| def download_input_files(input_files: list[str]) -> None: | ||
| """Download extract files from S3 to a local MinIO server. | ||
|
|
||
| For each file download, two AWS CLI commands are run by subprocess. | ||
| The output from the first command is piped to the second command. | ||
| These commands are further explained below: | ||
|
|
||
| 1. Copy the contents from the input file and direct to stdout. | ||
| ``` | ||
| aws s3 cp <input_file> - | ||
| ``` | ||
|
|
||
| 2. Given the stdout from the previous command as input, copy the contents | ||
| to a similarly named file on the local MinIO server. | ||
| ``` | ||
| aws s3 cp --endpoint-url <minio_s3_url> --profile minio - <input_file> | ||
| ``` | ||
|
|
||
| Note: An S3 client connected to the local MinIO server will check whether the | ||
| file exists prior to any download. | ||
| """ | ||
| s3_client = boto3.client( | ||
| "s3", | ||
| endpoint_url=CONFIG.minio_s3_url, | ||
| aws_access_key_id=CONFIG.minio_root_user, | ||
| aws_secret_access_key=CONFIG.minio_root_password, | ||
| ) | ||
|
|
||
| success_count = 0 | ||
| fail_count = 0 | ||
| for i, input_file in enumerate(input_files): | ||
| try: | ||
| download_input_file(input_file, s3_client) | ||
| success_count += 1 | ||
| logger.info( | ||
| f"Input file: {i + 1} / {len(input_files)}: '{input_file}' " | ||
| "available locally for transformation." | ||
| ) | ||
| except subprocess.CalledProcessError: | ||
| fail_count += 1 | ||
| logger.info( | ||
| f"Input file: {i + 1} / {len(input_files)}: '{input_file}' " | ||
| "failed to download." | ||
| ) | ||
| logger.info( | ||
| f"Available input files: {success_count}, missing input files: {fail_count}." | ||
| ) | ||
|
|
||
| if fail_count > 0: | ||
| raise RuntimeError( # noqa: TRY003 | ||
| f"{fail_count} input file(s) failed to download." | ||
| ) | ||
|
|
||
|
|
||
| def download_input_file(input_file: str, s3_client: S3Client) -> None: | ||
| if check_object_exists(CONFIG.TIMDEX_BUCKET, input_file, s3_client): | ||
| return | ||
| copy_command = ["aws", "s3", "cp", input_file, "-"] | ||
| upload_command = [ | ||
| "aws", | ||
| "s3", | ||
| "cp", | ||
| "--endpoint-url", | ||
| CONFIG.minio_s3_url, | ||
| "--profile", | ||
| "minio", | ||
| "-", | ||
| input_file, | ||
| ] | ||
| copy_process = subprocess.run(args=copy_command, check=True, capture_output=True) | ||
| subprocess.run( | ||
| args=upload_command, | ||
| check=True, | ||
| input=copy_process.stdout, | ||
| ) | ||
|
|
||
|
|
||
| def check_object_exists(bucket: str, input_file: str, s3_client: S3Client) -> bool: | ||
| key = input_file.replace(f"s3://{bucket}/", "") | ||
| try: | ||
| s3_client.head_object(Bucket=bucket, Key=key) | ||
| except ClientError as exception: | ||
| if exception.response["Error"]["Code"] == "404": | ||
| return False | ||
| return False | ||
| else: | ||
| return True | ||
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.
This looks great! I like the consistent logging structure, the final tally, and the exception raised if any failures. This will be helpful for debugging large amounts of files to download, if anything goes wrong.