├── LICENSE.md ├── README.md └── unzip.py /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Carlos Cárcamo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # aws-lambda-unzip-py 2 | Python AWS Lambda function to extract zip files uploaded to S3. 3 | 4 | The zip file will be deleted at the end of the operation. 5 | 6 | ## Permissions 7 | To remove the uploaded zip file, the role configured in your Lambda function should have a policy similar to this: 8 | 9 | ``` 10 | { 11 | "Effect": "Allow", 12 | "Action": [ 13 | "s3:GetObject", 14 | "s3:PutObject", 15 | "s3:DeleteObject" 16 | ], 17 | "Resource": [ 18 | "arn:aws:s3:::mybucket" 19 | ] 20 | } 21 | ``` 22 | 23 | # TODO 24 | You might know the limitations of AWS Lambda. The limitation of maximum execution duration per request could cause problems when unzipping large files, also consider the memory usage. 25 | 26 | * Improve performance (if possible) for large files 27 | * Extract files where the zip file was uploaded 28 | -------------------------------------------------------------------------------- /unzip.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | 3 | import urllib 4 | import zipfile 5 | import boto3 6 | import io 7 | 8 | 9 | print('Loading function') 10 | 11 | s3 = boto3.client('s3') 12 | bucket = 'my-bucket' 13 | 14 | def lambda_handler(event, context): 15 | key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key'].encode('utf8')) 16 | try: 17 | obj = s3.get_object(Bucket=bucket, Key=key) 18 | putObjects = [] 19 | with io.BytesIO(obj["Body"].read()) as tf: 20 | # rewind the file 21 | tf.seek(0) 22 | 23 | # Read the file as a zipfile and process the members 24 | with zipfile.ZipFile(tf, mode='r') as zipf: 25 | for file in zipf.infolist(): 26 | fileName = file.filename 27 | putFile = s3.put_object(Bucket=bucket, Key=fileName, Body=zipf.read(file)) 28 | putObjects.append(putFile) 29 | print(putFile) 30 | 31 | 32 | # Delete zip file after unzip 33 | if len(putObjects) > 0: 34 | deletedObj = s3.delete_object(Bucket=bucket, Key=key) 35 | print('deleted file:') 36 | print(deletedObj) 37 | 38 | except Exception as e: 39 | print(e) 40 | print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket)) 41 | raise e 42 | --------------------------------------------------------------------------------