Last active
May 20, 2024 06:27
-
-
Save aaron-ortega/32af61c8ce9c44cd5c17f9383b33e6b1 to your computer and use it in GitHub Desktop.
This file contains 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
import boto3 | |
from upath import UPath | |
def directories_with_boto3( | |
bucket: str, prefix: str, protocol: str = "s3" | |
) -> list[UPath] | None: | |
"""List the top level directories in the S3 bucket relative to the provided prefix.""" | |
path = UPath(bucket + prefix, protocol=protocol) | |
contents = [content for content in path.iterdir() if content.is_dir()] | |
if not contents: | |
return None | |
return contents | |
def directories_with_upath( | |
s3, bucket: str, prefix: str, delimiter: str = "/" | |
) -> list[str] | None: | |
"""Get the top level directory names with respect to the provided delimiter in the S3 bucket""" | |
result = s3.list_objects_v2(Bucket=bucket, Prefix=prefix, Delimiter=delimiter) | |
sub_dirs = result.get("CommonPrefixes") | |
if not sub_dirs: | |
return None | |
return sub_dirs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment