-
-
Save LucasRBarbosa/96239c83b0024e57beffb81c54d5356d to your computer and use it in GitHub Desktop.
Revisions
-
msharp created this gist
Feb 6, 2017 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,43 @@ import os import sys import re import boto3 import zipfile def parse_s3_uri(url): match = re.search('^s3://([^/]+)/(.+)', url) if match: return match.group(1), match.group(2) def unzip(event, context): source_bucket, source_key = parse_s3_uri(event['source']) destination_bucket, destination_key = parse_s3_uri(event['destination']) 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]))) 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 s3_client.upload_file(local_path, destination_bucket, s3_key) os.remove(local_path) return {"files": ['s3://' + destination_bucket + '/' + s for f,l,s in file_list]} 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,17 @@ import boto3 import json client = boto3.client('lambda', region_name='us-east-1') # unzip an archive def lambda_unzip(archive, destination): pl = {"source": archive, "destination": destination} response = client.invoke(FunctionName='unzipper', InvocationType='RequestResponse', Payload=json.dumps(pl)) print(response['Payload'].read()) archive = 's3://bucket-name/path/to/archive.zip' lambda_unzip(archive, 's3://bucket-name/path/to/destination/')