Created
May 2, 2018 15:24
-
-
Save davidejones/1e2b7350789aa9172fe0b1de0f4be5a1 to your computer and use it in GitHub Desktop.
aws lambda parsing multipart form with python3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from cgi import FieldStorage | |
from io import BytesIO | |
def parse_into_field_storage(fp, ctype, clength): | |
fs = FieldStorage( | |
fp=fp, | |
environ={'REQUEST_METHOD': 'POST'}, | |
headers={ | |
'content-type': ctype, | |
'content-length': clength | |
}, | |
keep_blank_values=True | |
) | |
form = {} | |
files = {} | |
for f in fs.list: | |
if f.filename: | |
files.setdefault(f.name, []).append(f) | |
else: | |
form.setdefault(f.name, []).append(f.value) | |
return form, files | |
def handler(event, context): | |
body_file = BytesIO(bytes(event["body"], "utf-8")) | |
form, files = parse_into_field_storage( | |
body_file, | |
event['headers']['content_type'], | |
body_file.getbuffer().nbytes | |
) | |
print(form) | |
print(files) | |
return { | |
"statusCode": 200, | |
"body": json.dumps({}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from cgi import parse_header, parse_multipart | |
import json | |
def handler(event, context): | |
content_type = event['headers'].get('Content-Type', '') or event['headers'].get('content-type', '') | |
c_type, c_data = parse_header(content_type) | |
c_data["boundary"] = bytes(c_data["boundary"], "utf-8") | |
body_file = BytesIO(bytes(event["body"], "utf-8")) | |
form_data = parse_multipart(body_file, c_data) | |
print(form_data) | |
return { | |
"statusCode": 200, | |
"body": json.dumps({}) | |
} |
Getting an error that says
ERROR: Could not find a version that satisfies the requirement cgi (from versions: none)
ERROR: No matching distribution found for cgi
cgi is built in https://docs.python.org/3/library/cgi.html so you don't need to pip install it
Seems like cgi is somehow corrupting the files when reading from multipart/form-data
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Getting an error that says