-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload_pictures
64 lines (52 loc) · 1.9 KB
/
upload_pictures
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
58
59
60
61
62
63
64
#!/usr/bin/python3
import os
import smtplib
import cgi
import cgitb
from email.message import EmailMessage
import boto3
#import logging
#logging.basicConfig(filename='/var/log/motion.log',level=logging.DEBUG)
#logging.debug('Request received')
cgitb.enable()
print("content-type: text/html\r\n\r\n")
post_result = cgi.FieldStorage(keep_blank_values=True)
bucket_name = "driving-test-bucket"
s3_resource = boto3.resource('s3',
aws_access_key_id="REDACTED",
aws_secret_access_key="REDACTED",
region_name="us-east-2"
)
first_bucket = s3_resource.Bucket(name=bucket_name)
print("File received")
print("""
HTTP/1.1 200 OK
Content-Length: 0
""")
# print(post_result.getlist("data"))
email_address = post_result.getvalue("email_address")
filename_from_post = post_result.getvalue("filename")
filename_from_post = os.path.basename(filename_from_post)
#logging.debug('filename: {}'.format(filename_from_post))
path = "/var/www/files"
msg = EmailMessage()
msg["Subject"] = "Photo from the raspberry pi"
msg["From"] = "example.com"
msg["To"] = email_address if email_address else "example@gmail.com"
print(os.path.join(path, filename_from_post))
print(post_result.keys())
print(post_result.getvalue("data"))
with open(os.path.join(path, filename_from_post), "wb") as filehandle:
print("about to write to file")
filehandle.write(post_result.getvalue("data"))
with open(os.path.join(path, filename_from_post), "rb") as filehandle:
img_data = filehandle.read()
fqn = os.path.join(path, filename_from_post)
first_bucket.upload_file(Filename=fqn, Key=filename_from_post)
msg.add_attachment(img_data, maintype='image', subtype="png")
print("arrived")
# Send the email via our own SMTP server.
with smtplib.SMTP('localhost') as s:
print("Beggining to send the email")
s.set_debuglevel(1)
s.send_message(msg)