Skip to content

Instantly share code, notes, and snippets.

@vincentclaes
Last active September 12, 2019 08:27
Show Gist options
  • Save vincentclaes/21d1e6dd7bf394126c27c25b42c5afb5 to your computer and use it in GitHub Desktop.
Save vincentclaes/21d1e6dd7bf394126c27c25b42c5afb5 to your computer and use it in GitHub Desktop.
def handler(event, context):
"""
extract the contents from the emails and
put the resulting object back to s3.
"""
# get the source key and bucket from
# the event that triggered this lambda_function function.
source_key = event['Records'][0]['s3']['object']['key']
source_bucket = event['Records'][0]['s3']['bucket']['name']
# get the email name from the key
email_name = source_key.split('/')[-1]
# get email from s3,
s3_client = boto3.client('s3')
s3_object = s3_client.get_object(Bucket=source_bucket,
Key=source_key)["Body"].read()
# extract the contents
email_object = extract_contents(s3_object, email_name)
# get the destination key and bucket
# from the environment variables
destination_bucket = os.getenv('DEST_BUCKET')
destination_key = os.getenv('DEST_KEY')
full_s3_key = destination_key + email_name + '.json'
# dump the result back to s3
s3_client.put_object(Body=email_object,
Bucket=destination_bucket,
Key=full_s3_key)
return full_s3_key
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment