4
from pathlib import Path
5
from typing import Any, List
10
def zip_folder(folder_to_zip: Path, dest_file_base_name: Path) -> Path:
12
Returns the path to the resulting zip file, with the appropriate extension added if needed
15
if dest_file_base_name.suffix == ".zip":
16
dest_file_base_name = dest_file_base_name.with_suffix("")
18
ensure_dir_exists(dest_file_base_name.parent)
20
print(f"Zipping {folder_to_zip}\n to {dest_file_base_name}")
22
return Path(shutil.make_archive(str(dest_file_base_name), "zip", folder_to_zip))
25
def unzip_folder(zip_file_path: Path, unzip_to_folder: Path) -> None:
27
Returns the path to the unzipped folder
29
print(f"Unzipping {zip_file_path}")
30
print(f" to {unzip_to_folder}")
31
shutil.unpack_archive(zip_file_path, unzip_to_folder, "zip")
34
def ensure_dir_exists(dir: Path) -> None:
35
dir.mkdir(parents=True, exist_ok=True)
38
def copy_file(source_file: Path, dest_file: Path) -> None:
39
ensure_dir_exists(dest_file.parent)
40
shutil.copyfile(source_file, dest_file)
43
def load_json_file(file_path: Path) -> Any:
45
Returns the deserialized json object
47
with open(file_path) as f:
51
def write_json_file(file_path: Path, content: Any) -> None:
52
dir = file_path.parent
53
ensure_dir_exists(dir)
55
with open(file_path, "w") as f:
56
json.dump(content, f, indent=2)
59
def sanitize_for_s3(text: str) -> str:
61
S3 keys can only contain alphanumeric characters, underscores, and dashes.
62
This function replaces all other characters with underscores.
64
return re.sub(r"[^a-zA-Z0-9_-]", "_", text)
67
def upload_file_to_s3(file_name: Path, bucket: str, key: str) -> None:
68
print(f"Uploading {file_name}")
69
print(f" to s3://{bucket}/{key}")
71
boto3.client("s3").upload_file(
78
def download_s3_objects_with_prefix(
79
bucket_name: str, prefix: str, download_folder: Path
81
s3 = boto3.resource("s3")
82
bucket = s3.Bucket(bucket_name)
86
for obj in bucket.objects.filter(Prefix=prefix):
87
download_path = download_folder / obj.key
89
ensure_dir_exists(download_path.parent)
90
print(f"Downloading s3://{bucket.name}/{obj.key}")
91
print(f" to {download_path}")
93
s3.Object(bucket.name, obj.key).download_file(str(download_path))
94
downloads.append(download_path)
96
if len(downloads) == 0:
98
f"There were no files matching the prefix `{prefix}` in bucket `{bucket.name}`"