Skip to content

Instantly share code, notes, and snippets.

@AmSmart
Last active August 6, 2021 15:44
Show Gist options
  • Save AmSmart/3f3a703a1636b5795a39ed4259c7b5cf to your computer and use it in GitHub Desktop.
Save AmSmart/3f3a703a1636b5795a39ed4259c7b5cf to your computer and use it in GitHub Desktop.
Decompress a zip file in S3 into a folder and upload it back to S3 (via SageMaker)
import boto3
import os
from zipfile import ZipFile
# When running on SageMaker, need execution role
from sagemaker import get_execution_role
role = get_execution_role()
# Declare bucket name, remote file, and destination
my_bucket = ""
zip_file = ""
extracted_folder_name = ""
# Function to Upload folder to S3
def upload_folder_to_s3(s3bucket, inputDir, s3Path):
print("Uploading results to s3 initiated...")
print("Local Source:",inputDir)
os.system("ls -ltR " + inputDir)
print("Dest S3path:",s3Path)
try:
for path, subdirs, files in os.walk(inputDir):
for file in files:
dest_path = path.replace(inputDir,"")
__s3file = os.path.normpath(s3Path + '/' + dest_path + '/' + file)
__local_file = os.path.join(path, file)
print("upload : ", __local_file, " to Target: ", __s3file, end="")
s3bucket.upload_file(__local_file, __s3file)
print(" ...Success")
print("Folder Upload Comleted")
except Exception as e:
print(" ... Failed!! Quitting Upload!!")
print(e)
raise e
# Connect to S3 bucket and download file
s3 = boto3.resource('s3')
bucket = s3.Bucket(my_bucket)
bucket.download_file(zip_file, zip_file)
# Extact the zip file to a new folder
with ZipFile(zip_file, 'r') as zipObj:
zipObj.extractall(extracted_folder_name)
upload_folder_to_s3(s3.Bucket(my_bucket), extracted_folder_name, extracted_folder_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment