Created
October 29, 2021 11:01
-
-
Save jianshen92/63966c619c392c5ff5a7089880f43365 to your computer and use it in GitHub Desktop.
Download s3 directory into temporary directory locally
This file contains hidden or 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 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