Skip to content

Instantly share code, notes, and snippets.

@jonashaag
Created August 28, 2022 19:18
Show Gist options
  • Save jonashaag/a9f672fcb016e6df7de3bab31553eb02 to your computer and use it in GitHub Desktop.
Save jonashaag/a9f672fcb016e6df7de3bab31553eb02 to your computer and use it in GitHub Desktop.
boto3 copytree prefix
import shutil
from pathlib import Path
def s3_copytree(bucket, src_prefix: str, dst_folder: Path):
for s3_f in bucket.objects.filter(Prefix=src_prefix):
target_path = dst_folder / s3_f.key.removeprefix(src_prefix)
target_path.parent.mkdir(parents=True, exist_ok=True)
with open(target_path, "wb") as target_f:
shutil.copyfileobj(s3_f.get()["Body"], target_f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment