Skip to content

Commit

Permalink
Add presign and read_key to build.s3
Browse files Browse the repository at this point in the history
  • Loading branch information
shreve committed Aug 1, 2024
1 parent 85a1d51 commit 64a8ad6
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/mads/build/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

import boto3

s3 = boto3.client("s3")


def __getattr__(name):
return getattr(s3, name)


def find_keys(
bucket: str,
prefix: str = "",
contains: list[str] = [],
) -> list[str]:
list_objects = boto3.client("s3").get_paginator("list_objects_v2")
list_objects = s3.get_paginator("list_objects_v2")
paginator = list_objects.paginate(Bucket=bucket, Prefix=prefix)

results = []
Expand Down Expand Up @@ -36,3 +42,25 @@ def find_key(
for key in find_keys(bucket, prefix, contains):
return key
return None


def presign(
bucket: str,
keys: str | list[str],
expires_in: int = 12 * 60 * 60, # 12 hours
) -> dict[str, str]:
if isinstance(keys, str):
keys = [keys]

return {
key: s3.generate_presigned_url(
"get_object",
Params={"Bucket": bucket, "Key": key},
ExpiresIn=expires_in,
)
for key in keys
}


def read_key(bucket: str, key: str) -> str:
return s3.get_object(Bucket=bucket, Key=key)["Body"].read().decode()

0 comments on commit 64a8ad6

Please sign in to comment.