-
-
Save bhison/53e70f7c7f3ba07968250587f77a13e1 to your computer and use it in GitHub Desktop.
unzip archive from S3 on AWS Lambda, in place, retain contentType
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 os | |
import sys | |
import re | |
import boto3 | |
import zipfile | |
#Expand on this based on which filetypes you expect to recieve | |
mimeTypes = { | |
".html": "text/html", | |
".png": "image/png", | |
".jpg": "image/jpg" | |
} | |
def unzip(event, context): | |
source_bucket = event['Records'][0]['s3']['bucket']['name'] | |
source_key = event['Records'][0]['s3']['object']['key'] | |
destination_bucket = source_bucket | |
destination_key = os.path.dirname(source_key) | |
temp_zip = '/tmp/file.zip' | |
s3_client = boto3.client('s3') | |
print("downloading {}".format(source_key)) | |
s3_client.download_file(source_bucket, source_key, temp_zip) | |
zfile = zipfile.ZipFile(temp_zip) | |
file_list = [( name, | |
'/tmp/' + os.path.basename(name), | |
destination_key + "/" + os.path.basename(name)) | |
for name in zfile.namelist()] | |
print("got names {}".format("; ".join([n for n,b,d in file_list]))) | |
print(file_list) | |
for file_name, local_path, s3_key in file_list: | |
data = zfile.read(file_name) | |
with open(local_path, 'wb') as f: | |
f.write(data) | |
del(data) # free up some memory | |
extension = split_tup = os.path.splitext(s3_key)[1]; | |
contentType = mimeTypes[extension] | |
s3_client.upload_file(local_path, destination_bucket, s3_key, ExtraArgs={'ContentType': contentType}) | |
os.remove(local_path) | |
return {"files": ['s3://' + destination_bucket + '/' + s for f,l,s in file_list]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment