Skip to content

Instantly share code, notes, and snippets.

@davidejones
Created May 2, 2018 15:24
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save davidejones/1e2b7350789aa9172fe0b1de0f4be5a1 to your computer and use it in GitHub Desktop.
Save davidejones/1e2b7350789aa9172fe0b1de0f4be5a1 to your computer and use it in GitHub Desktop.
aws lambda parsing multipart form with python3
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({})
}
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({})
}
@punit1108
Copy link

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

@davidejones
Copy link
Author

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

@punit1108
Copy link

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