Skip to content

Instantly share code, notes, and snippets.

@msharp
Created February 6, 2017 22:52
Show Gist options
  • Save msharp/eaa1c5b1b4e50f1372c19bbe5ef52f9e to your computer and use it in GitHub Desktop.
Save msharp/eaa1c5b1b4e50f1372c19bbe5ef52f9e to your computer and use it in GitHub Desktop.
unzip archive from S3 on AWS Lambda
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]}
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/')
@vcmgc27
Copy link

vcmgc27 commented Jul 19, 2021

Hi,
Need an help in using it.
How to deploy this script?
It would be helpful, if you give the steps for using it.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment