Skip to content

Instantly share code, notes, and snippets.

@jianshen92
Created October 29, 2021 11:01
Show Gist options
  • Save jianshen92/63966c619c392c5ff5a7089880f43365 to your computer and use it in GitHub Desktop.
Save jianshen92/63966c619c392c5ff5a7089880f43365 to your computer and use it in GitHub Desktop.
Download s3 directory into temporary directory locally
import boto3
def download_s3_directory(s3_resource, bucket: str, key: str, local_dir: str):
""" Download the S3 directory to a local disk """
bucket_obj = s3_resource.Bucket(bucket)
if key != "" and not key.endswith("/"):
key = key + "/"
for obj in bucket_obj.objects.filter(Prefix=key):
local_file_path = local_dir + "/" + obj.key[len(key) :]
local_file_dir = os.path.dirname(local_file_path)
os.makedirs(local_file_dir, exist_ok=True)
bucket_obj.download_file(obj.key, local_file_path)
s3_resource = boto3.resource("s3")
bucket = 's3_bucket_name'
key = 's3_bucket_key'
with tempfile.TemporaryDirectory() as temp_dir:
download_s3_directory(s3_resource, bucket, key, temp_dir)r)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment