Created
July 25, 2017 17:09
-
-
Save filipececcon/4957d8d8922d209f6a96ebe7abbb168b to your computer and use it in GitHub Desktop.
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 | |
import urllib | |
import zipfile | |
import uuid | |
import os | |
import shutil | |
def lambda_handler(event, context): | |
print("[lambda_handler] Start") | |
bucket_name = event['Records'][0]['s3']['bucket']['name'] | |
key_name = urllib.unquote_plus(event['Records'][0]['s3']['object']['key']).decode('utf8') | |
try: | |
s3 = boto3.client('s3') | |
#-------------------------------------------------------------------------------------------------------------- | |
print("[lambda_handler] \tDownload file from S3") | |
#-------------------------------------------------------------------------------------------------------------- | |
local_file_path = '/tmp/' + key_name.split('/')[-1] | |
s3.download_file(bucket_name, key_name, local_file_path) | |
#-------------------------------------------------------------------------------------------------------------- | |
print("[lambda_handler] \tExtracting files") | |
#-------------------------------------------------------------------------------------------------------------- | |
output_folder = local_file_path.rsplit('.')[0] + "-" + str(uuid.uuid4()) | |
output_folder = output_folder[:99] | |
zf = zipfile.ZipFile(local_file_path) | |
zf.extractall(path=output_folder) | |
#-------------------------------------------------------------------------------------------------------------- | |
print("[lambda_handler] \tUploading files") | |
#-------------------------------------------------------------------------------------------------------------- | |
for root, dirs, files in os.walk(output_folder): | |
for f in files: | |
output_file_name = (os.path.join(root, f)).replace(output_folder+'/', '') | |
print("[lambda_handler] \t\tUploading file: %s"%output_file_name) | |
s3.upload_file(os.path.join(root, f), bucket_name, output_file_name) | |
#-------------------------------------------------------------------------------------------------------------- | |
print("[lambda_handler] \tDelete Local files. Folder: %s"%output_folder) | |
#-------------------------------------------------------------------------------------------------------------- | |
shutil.rmtree(output_folder) | |
os.remove(local_file_path) | |
#-------------------------------------------------------------------------------------------------------------- | |
print("[lambda_handler] \tDelete Original file") | |
#-------------------------------------------------------------------------------------------------------------- | |
#SE QUISER QUE O ZIP SEJA EXCLUÍDO APOS A EXTRAÇÃO DESCOMENTE A LINHA ABAIXO | |
#s3.delete_object(Bucket=bucket_name, Key=key_name) | |
except Exception as e: | |
print "Error to process file" | |
print e | |
print("[lambda_handler] End") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment