Created
August 28, 2022 19:18
-
-
Save jonashaag/a9f672fcb016e6df7de3bab31553eb02 to your computer and use it in GitHub Desktop.
boto3 copytree prefix
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 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