How to get POST data from file upload form with AWS Lambda in python

Viewed 34

I have a simple AWS Lambda function that presents an HTML form with a file upload input. I cannot figure out how to get the file data after the form has been submitted. I can get regular text data from text inputs, but not the binary data from a file upload. I've heard that there is something that can be done with API Gateway, but I was trying to avoid that for now because of the 30-second timeout. Does anyone know how to get the binary file data that is posted from this form? I am using a function URL to access (technically not a trigger, but that is what triggers the function) the form.

import json
import os
import logging
from xml.etree import ElementTree as ET

logger = logging.getLogger()
logger.setLevel(logging.INFO) # ALL TRACE DEBUG INFO WARN ERROR FATAL OFF

def lambda_handler(event, context):
    logger.info(str(event))
    div = ET.Element('div')
    p = ET.SubElement(div, 'p')
    p.text = 'test'
    form = ET.SubElement(p, 'form', attrib={'method': 'POST', 'enctype': 'multipart/form-data', 'action': '/'})
    input = ET.SubElement(form, 'input', attrib={'type': 'file', 'name': 'filename'})
    input = ET.SubElement(form, 'input', attrib={'type': 'submit', 'value': 'Update Profile'})
    a = os.listdir('/tmp')
    logger.info(str(a))
    return {
        'statusCode': 200,
        'headers': {'Content-Type': 'text/html'},
        'body': ET.tostring(div, encoding='utf8', method='html').decode('utf8')
    }
0 Answers
Related