1

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')
    }

1 Answer 1

1

I'd highly advise you to use direct S3 upload using one of the AWS SDKs. AWS Lambda is best suited for processing events, not content transfers like uploads.

Or you can also use API Gateway if you really need to make a direct content transfer to the Lambda.

Sign up to request clarification or add additional context in comments.

1 Comment

The best solution for my use case was to generate a AWS S3 pre-signed POST URL in the page that presents the form. And then have the form in that same page have a POST method upon submit. Call me if you need a better explanation. 804-399-8069

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.