Skip to content

Question about file upload functionality #3

@montao

Description

@montao

Hello!

I use serverless framework with AWS Lambda and a python backend. A bit surprised that there was only low-level (using the python cgi package) to receive and parse file uploads from a HTTP form post:

from cgi import parse_header, parse_multipart
from io import BytesIO
import uuid
import boto3
s3 = boto3.resource('s3')
# TODO: Use case-insensitive headers
c_type, c_data = parse_header(event['headers']['content-type'])
c_data['boundary'] = bytes(c_data['boundary'], 'utf8')
body_file = BytesIO(bytes(event['body'], 'utf8'))
if 'CONTENT-LENGTH' not in c_data:
    # TODO: Do this properly. Why is it needed?
    c_data['CONTENT-LENGTH'] = "9999999"  
form_data = parse_multipart(body_file, c_data)
# TODO: Loop here for several file uploads
data = form_data['file'][0].decode('UTF-8')  
identifier = str(uuid.uuid4())
s3.Bucket(settings.get_bucket()).put_object(Key=f"{identifier}.json",Body=data)

It looks like flask has built-in functionality to do it, e.g. a described: https://pythonhosted.org/Flask-Uploads/. My question is if I can use the flask built-in functionality for file upload if I use serverless-flask, or if something else is preferred way instead?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions