Skip to content

Commit

Permalink
Improve S3 find_keys searching
Browse files Browse the repository at this point in the history
  • Loading branch information
shreve committed Jul 22, 2024
1 parent 25014e9 commit 44017a4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ dependencies = [
[project.optional-dependencies]
test = ["pytest"]


[project.scripts]
mads = "mads.cli:main"

Expand Down
21 changes: 16 additions & 5 deletions src/mads/build/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,32 @@
def find_keys(
bucket: str,
prefix: str = "",
contains: list[str] = [""],
contains: list[str] = [],
) -> list[str]:
list_objects = boto3.client("s3").get_paginator("list_objects_v2")
paginator = list_objects.paginate(Bucket=bucket, Prefix=prefix)
queries = [f"contains(Key, '{term}')" for term in contains]
search = f"Contents[?{' && '.join(queries)}].Key"
results = paginator.search(search)

results = []

if contains and not isinstance(contains, list):
contains = [contains]

if contains:
queries = [f"contains(Key, '{term}')" for term in contains]
search = f"Contents[?{' && '.join(queries)}].Key"
results = paginator.search(search)
else:
results = [
entry.get("Key") for page in paginator for entry in page.get("Contents", [])
]

return list(results)


def find_key(
bucket: str,
prefix: str = "",
contains: list[str] = [""],
contains: list[str] = [],
) -> str | None:
for key in find_keys(bucket, prefix, contains):
return key
Expand Down

0 comments on commit 44017a4

Please sign in to comment.