Skip to content

Instantly share code, notes, and snippets.

@LucasRBarbosa
Forked from msharp/aws-lambda-unzipper.py
Created February 12, 2021 18:30
Show Gist options
  • Select an option

  • Save LucasRBarbosa/96239c83b0024e57beffb81c54d5356d to your computer and use it in GitHub Desktop.

Select an option

Save LucasRBarbosa/96239c83b0024e57beffb81c54d5356d to your computer and use it in GitHub Desktop.

Revisions

  1. @msharp msharp created this gist Feb 6, 2017.
    43 changes: 43 additions & 0 deletions aws-lambda-unzipper.py
    Original 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]}

    17 changes: 17 additions & 0 deletions invoke-unzipper.py
    Original 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/')