-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrawemaillambda.py
57 lines (46 loc) · 1.82 KB
/
rawemaillambda.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from __future__ import print_function
import email
import zipfile
import os
import gzip
import string
import boto3
import urllib
import logging
import boto3
import json
logger = logging.getLogger()
logger.setLevel(logging.INFO)
s3 = boto3.client('s3')
s3r = boto3.resource('s3')
def lambda_handler(event, context):
bucket_name = event['Records'][0]['s3']['bucket']['name']
file_key = event['Records'][0]['s3']['object']['key']
logger.info('Reading {} from {}'.format(file_key, bucket_name))
# Read the raw text file into a Email Object
response = s3r.Bucket(bucket_name).Object(file_key)
message = email.message_from_string(response.get()["Body"].read())
#pull the attachment and check the content type
#here's the place to add if / else logic to limit to particular attachment types
attachment = message.get_payload()[1]
attachment.get_content_type()
contentdisp = str.split(attachment.get('Content-Disposition'), '=') #grab attachment name
fname = contentdisp[1].replace('\"', '')
#should probably write these to a /tmp/ directory then blow it away later
tmpfname = 'tmp' + fname
open('/tmp/' + tmpfname, 'w').write(attachment.get_payload())
jpgtxttmp = open('/tmp/' + tmpfname,'rb').read().replace('\n','') #strip whitespace
processfname = 'prc' + fname
jpgprocess = open('/tmp/' + processfname, 'w') #open the stream for the tmp file
jpgprocess.write(jpgtxttmp) #write the temp flow to it
jpgprocess.close()
newjpgtxt = open('/tmp/' + processfname, 'rb').read()
jpgprocessed = open('/tmp/' + fname, 'w')
jpgprocessed.write(newjpgtxt.decode('base64')) #write the decoded txt
jpgprocessed.close()
#write to s3
s3.upload_file('/tmp/' + fname, bucket_name, fname)
return {
'statusCode': 200,
'body': json.dumps('Lambda success!')
}