-
-
Save KrustyHack/1748e61591f44e373a532acd582d4502 to your computer and use it in GitHub Desktop.
unzip archive from S3 on AWS Lambda
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 | |
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 characters
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/') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment