I have created static web site which I am planning to host in S3 bucket. This HTML has a form and attachment feature which will be sent to my email id when user will attach a file and hit 'Upload'. Below is my HTML code snippets -
<form enctype="multipart/form-data" name="fileinfo">
<label>Your email address:</label>
<input id = "email "type="email" autocomplete="on" autofocus name="userid" placeholder="email" required size="32" maxlength="64" /><br />
<label>Custom file label:</label>
<input type="text" name="filelabel" size="12" maxlength="32" /><br />
<label>File to stash:</label>
<input type="file" name="file" id = "fileUpload" required />
<input type="button" id = "upload" value="Upload!" />
</form>
and then Jquery to trigger POST request to AWS API gateway
$('#upload').on("click", function (){
var oOutput = document.querySelector("div"),
oData = new FormData(form);
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// Typical action to be performed when the document is ready:
console.log(xhttp.responseText);
console.log(xhttp.statusText);
console.log(xhttp.status);
oOutput.innerHTML = xhttp.responseText;
} else {
oOutput.innerHTML = "Error " + xhttp.status + " occurred when trying to upload your file.<br \/>";
}
};
xhttp.open("POST", "https://myLambdaAPIURL", true);
xhttp.setRequestHeader("Content-type", "application/json;charset=UTF-8");
xhttp.send(JSON.stringify({Email:$('#email').val(),fileUpload:$('#fileUpload').val()}));
API gateway in AWS has all the default configurations to process the json format.
My AWS lambda code which works fine to send just an email.
import smtplib , os
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.mime.base import MIMEBase
from email.message import Message
from email.encoders import encode_base64
from email.mime.text import MIMEText
from mimetypes import guess_type
def lambda_handler(event, context):
msg = MIMEMultipart()
msg['Subject'] = 'File Uploaded'
msg['From'] = 'user@gmail.com'
msg['To'] = 'user@gmail.com'
emailFrom = "user@gmail.com"
emailTo ="user@gmail.com"
for file in event['fileUpload']:
mimetype, encoding = guess_type(file)
if mimetype == None:
mimetype = "text/plain"
mimetype = mimetype.split('/',1)
fp = open(file, "rb")
attachment.set_payload(fp.read())
fp.close()
encode_base64(attachment)
attachment.add_header('Content-Disposition', 'attachment', file=os.path.basename(file))
msg.attach(attachment)
s = smtplib.SMTP('smtp.gmail.com:587')
s.starttls()
s.login('user@gmail.com','password')
response = s.sendmail(emailFrom, emailTo, msg.as_string())
s.quit()
return response
But when I attach a file and try to process it, the fake path error comes
{"stackTrace": [["/var/task/pyLambda.py", 24, "lambda_handler", "fo = open(filename, \"rb\")"]], "errorType": "IOError", "errorMessage": "[Errno 2] No such file or directory: u'C:\\fakepath\\testing.txt'"}
so I am thinking may be I can use \tmp directory in AWS lambda but not sure how to do that.
Any help or any guidance is highly appreciated.