-
Notifications
You must be signed in to change notification settings - Fork 1
Fix/period in folder #27
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
Changes from all commits
bddb86c
1596557
5d5f377
d33f1ed
b430e40
fa607f5
2c23d6f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| """Semantic versioning for S3MP.""" | ||
|
|
||
| __version__ = "0.5.3" | ||
| __version__ = "0.6.0" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,8 +3,7 @@ | |
| from pathlib import Path | ||
| from S3MP.global_config import S3MPConfig | ||
| from S3MP.types import S3Bucket, S3Client, S3ListObjectV2Output | ||
| from botocore.exceptions import ClientError | ||
|
|
||
| from typing import List | ||
|
|
||
| def s3_list_single_key( | ||
| key: str, | ||
|
|
@@ -20,10 +19,9 @@ def s3_list_single_key( | |
|
|
||
| def s3_list_child_keys( | ||
| key: str, | ||
| bucket=None, | ||
| client=None, | ||
| continuation_token=None, | ||
| ): | ||
| bucket: S3Bucket = None, | ||
| client: S3Client = None, | ||
| ) -> List[str]: | ||
| """List details of all child keys on S3.""" | ||
| if not key.endswith("/"): | ||
| warnings.warn(f"Listing child keys of {key} - key does not end with '/'") | ||
|
|
@@ -34,9 +32,29 @@ def s3_list_child_keys( | |
| "Prefix": key, | ||
| "Delimiter": "/", | ||
| } | ||
| if continuation_token: | ||
| params["ContinuationToken"] = continuation_token | ||
| return client.list_objects_v2(**params) | ||
| child_s3_keys: List[str] = [] | ||
| continuation_token: str = None | ||
| while True: | ||
| if continuation_token: | ||
| params["ContinuationToken"] = continuation_token | ||
|
|
||
| resp = client.list_objects_v2(**params) | ||
|
|
||
| # Collect keys from the current response | ||
| if "Contents" in resp: | ||
| child_s3_keys.extend( | ||
| obj["Key"] for obj in resp["Contents"] if obj["Key"] != key | ||
| ) | ||
| if "CommonPrefixes" in resp: | ||
| child_s3_keys.extend(obj["Prefix"] for obj in resp["CommonPrefixes"]) | ||
|
|
||
| # Check if there are more pages to fetch | ||
| if "NextContinuationToken" in resp: | ||
| continuation_token = resp["NextContinuationToken"] | ||
| else: | ||
| break | ||
|
|
||
| return child_s3_keys | ||
|
|
||
| def download_key( | ||
| key: str, | ||
|
|
@@ -47,12 +65,18 @@ def download_key( | |
| """Download a key from S3.""" | ||
| bucket = bucket or S3MPConfig.bucket | ||
| client = client or S3MPConfig.s3_client | ||
|
|
||
| if not key_exists_on_s3(key, bucket, client): | ||
| raise ValueError(f"Key {key} does not exist on S3") | ||
|
|
||
| # If the key is a file, download it | ||
| # Otherwise, download all child keys | ||
| if key_is_file_on_s3(key, bucket, client): | ||
| local_path.parent.mkdir(parents=True, exist_ok=True) | ||
| client.download_file(bucket.name, key, str(local_path), Callback=S3MPConfig.callback, Config=S3MPConfig.transfer_config) | ||
| else: | ||
| for obj in s3_list_child_keys(key, bucket, client)["Contents"]: | ||
| download_key(obj["Key"], local_path / obj["Key"].replace(key, "")) | ||
| for child_key in s3_list_child_keys(key, bucket, client): | ||
| download_key(child_key, local_path / child_key.replace(key, "")) | ||
|
|
||
| def upload_to_key( | ||
| key: str, | ||
|
|
@@ -86,20 +110,19 @@ def key_is_file_on_s3( | |
| bucket: S3Bucket = None, | ||
| client: S3Client = None, | ||
| ) -> bool: | ||
| """Check if a key is a file on S3, returns false if it is a folder. Raises an error if the key does not exist.""" | ||
| """Check if a key is a file on S3 by using head_object.""" | ||
| bucket = bucket or S3MPConfig.bucket | ||
| client = client or S3MPConfig.s3_client | ||
| if not key_exists_on_s3(key, bucket, client): | ||
| raise ValueError(f"Key {key} does not exist on S3") | ||
| res = s3_list_single_key(key, bucket, client) | ||
| # Handle case of trailing slash, but still verify | ||
| if ( | ||
| key[-1] == "/" | ||
| and len(res['Contents']) == 1 | ||
| and res['Contents'][0]['Key'] == key | ||
| ): | ||
| return False | ||
| return "Contents" in res | ||
|
|
||
| try: | ||
| client.head_object(Bucket=bucket.name, Key=key) | ||
| return True | ||
| except Exception as e: | ||
| # 404 occurs if the key is a "folder" or does not exist | ||
| if "404" in str(e): | ||
| return False | ||
|
Comment on lines
+120
to
+123
Collaborator
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. Is there a specific exception that could be caught?
Member
Author
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. probably yeah, i initially tried using the botocore ClientError exception but for some reason it wasn't catching properly
Collaborator
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. I'll just tag #23 and let it slow-cook on the backburner |
||
| else: | ||
| raise e | ||
|
|
||
|
|
||
| def key_size_on_s3( | ||
|
|
@@ -124,8 +147,8 @@ def delete_child_keys_on_s3( | |
| """Delete all keys that are children of a key on S3.""" | ||
| bucket = bucket or S3MPConfig.bucket | ||
| client = client or S3MPConfig.s3_client | ||
| for obj in s3_list_child_keys(key, bucket, client)["Contents"]: | ||
| client.delete_object(Bucket=bucket.name, Key=obj["Key"]) | ||
| for child_key in s3_list_child_keys(key, bucket, client): | ||
| client.delete_object(Bucket=bucket.name, Key=child_key) | ||
|
|
||
|
|
||
| def delete_key_on_s3( | ||
|
|
||
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.
Could we just check the
is_fileproperty on the last key segment, or is it possible that it's inconsistent?Technically S3 objects don't need extensions at all, so checking for "." seems problematic (which is my own fault).
Uh oh!
There was an error while loading. Please reload this page.
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.
yeah as far as i can tell the
is_fileproperty has to be explicitly set currently (it isn't set when initing a MirrorPath)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.
😭