Skip to content

Instantly share code, notes, and snippets.

@shubhpy
Last active April 4, 2024 08:18
Show Gist options
  • Save shubhpy/5767c21da8b30db0f06b11fafc46b63e to your computer and use it in GitHub Desktop.
Save shubhpy/5767c21da8b30db0f06b11fafc46b63e to your computer and use it in GitHub Desktop.
Python script to download a S3 folder on the web. This file downloads all files in the userImages folder and save locally on the backend server. Later zipped folder is uploaded back on the S3 and download link is generated.
#!/usr/bin/env python3
import time
import os
import zipfile
import shutil
import errno
folder_name = "local_folder_name" #_where_all_files need to save
DOWNLOAD_LOCATION_PATH = os.path.expanduser("~") + folder_name + "/"
if not os.path.exists(DOWNLOAD_LOCATION_PATH):
os.makedirs(DOWNLOAD_LOCATION_PATH)
""""
Say i have to download all users content in a particular project
whose location on S3 is at
Organization/Project/UserImages/user1.jpeg
Organization/Project/UserImages/user2.jpeg
Organization/Project/UserImages/user3/user3Passport.jpeg
Organization/Project/UserImages/user3/user3Casual.jpeg
.
.
.
"""
#SO
Prefix = "Organization/Project/UserImages/"
#make yuor s3cleint
bucket_list = s3client.list_objects(Bucket=bucket_name,Prefix=Prefix)
for l in bucket_list['Contents']:
key_string = str(l["Key"])
s3_path = DOWNLOAD_LOCATION_PATH + key_string.split(Prefix)[-1]
try:
s3client.download_file(bucket_name, key_string, s3_path)
except Exception as exc:
if not os.path.exists(makdir):
try:
os.makedirs(makdir)
except Exception as e:
if exc.errno != errno.EEXIST:
raise
zipfile_name = folder_name+'.zip'
zipfile_path = os.path.expanduser("~") + "/" + zipfile_name
zipf = zipfile.ZipFile( zipfile_path, 'w', zipfile.ZIP_DEFLATED)
zipdir = DOWNLOAD_LOCATION_PATH
for root, dirs, files in os.walk(zipdir):
for file in files:
zipf.write(os.path.join(root, file))
zipf.close()
key = "Zip-Files/" + zipfile_name
s3client.upload_file(Filename=zipfile_path, Bucket = bucket_name, Key = key)
url = s3client.generate_presigned_url('get_object', {'Bucket': bucket_name, 'Key': key,'ResponseContentDisposition':'attachment; filename='+zipfile_name},ExpiresIn=3000)
print (url)
os.remove(zipfile_path)
shutil.rmtree(DOWNLOAD_LOCATION_PATH)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment