Skip to content

Instantly share code, notes, and snippets.

@ari-vedant-jain
Created September 5, 2021 21:09
Show Gist options
  • Save ari-vedant-jain/3ab00e625e93fca20b6897734a6023b9 to your computer and use it in GitHub Desktop.
Save ari-vedant-jain/3ab00e625e93fca20b6897734a6023b9 to your computer and use it in GitHub Desktop.
Uncompress Zip files in S3 using Python
import boto3
import zipfile
from datetime import *
from io import BytesIO
import json
import re
def unzip_file():
session = boto3.Session()
dev_client = session.client('s3')
dev_resource=boto3.resource('s3')
S3_ZIP_FOLDER = '<sourceFolder/>'
S3_UNZIPPED_FOLDER = '<destinationFolder/>'
S3_BUCKET = '<bucketname>'
ZIP_FILE='<zipfile.zip>'
bucket_dev = dev_resource.Bucket(S3_BUCKET)
zip_obj = dev_resource.Object(bucket_name=S3_BUCKET, key=f"{S3_ZIP_FOLDER}{ZIP_FILE}")
print("zip_obj=",zip_obj)
buffer = BytesIO(zip_obj.get()["Body"].read())
z = zipfile.ZipFile(buffer)
#
# for each file within the zip
for filename in z.namelist():
file_info = z.getinfo(filename)
# Now copy the files to the 'unzipped' S3 folder
print(f"Copying file {filename} to {S3_BUCKET}/{S3_UNZIPPED_FOLDER}{filename}")
response = dev_client.put_object(
Body=z.open(filename).read() ,
# might need to replace above line with the one
# below for windows files
#
# Body=z.open(filename).read().decode("iso-8859-1").encode(encoding='UTF-8'),
Bucket=S3_BUCKET,
Key=f'{S3_UNZIPPED_FOLDER}{filename}'
)
print(f"Done Unzipping {ZIP_FILE}")
unzip_file()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment