Python Forum
parsing mutipart form data in Lambda
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
parsing mutipart form data in Lambda
#1
I am working on developing an AWS Lambda function in python. This function is behind AWS API Gateway.
I upload two files from the URL and in lambda function I get event data as below.
I need help how to parse this response and save the two files in S3.


----------------------------124218046032878137249340
Content-Disposition: form-data; name="file1"; filename="2.csv"

Content-Type: text/csv

header,value

a,1
b,2

----------------------------124218046032878137249340

Content-Disposition: form-data; name="file2"; filename="1.txt"

Content-Type: text/plain

aa
bb
cc

----------------------------124218046032878137249340--
Reply
#2
If it's a HTTP response could use Requests to parse it.
Data after Content-Type: text/plain can get with .text or .contend.
>>> import requests
>>> r = requests.get('http://httpbin.org/')
>>> r.status_code
200
>>> r.headers['content-type']
'text/html; charset=utf-8'

>>> print(r.text[:95])
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv='content-type' value='text/html;charset=utf8'>
Reply
#3
Its not a HTTP response, in the service side code . There is a Lambda function , that get the data as bytes array stream.
Reply
#4
Depends on what you want,quick and dirty way to get contend with regex.
import re

data = '''\
----------------------------124218046032878137249340
Content-Disposition: form-data; name="file1"; filename="2.csv"

Content-Type: text/csv

header,value

a,1
b,2

----------------------------124218046032878137249340

Content-Disposition: form-data; name="file2"; filename="1.txt"

Content-Type: text/plain

aa
bb
cc'''

r_csv = re.search(r'Content-Type: text/csv(.*\,\w)', data, re.DOTALL)
lst = [i for i in r_csv.group(1).split('\n') if i != '']
result = [i.split(',') for i in lst]
print(result)
print('--------------------------')
r_text = re.search(r'Content-Type: text/plain(.*)', data, re.DOTALL)
print([i for i in r_text.group(1).split('\n') if i != ''])
Output:
[['header', 'value'], ['a', '1'], ['b', '2']] -------------------------- ['aa', 'bb', 'cc']
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sending data from the form to DB quisatz 6 1,301 Nov-13-2023, 09:23 AM
Last Post: Annaat20
Video doing data treatment on a file import-parsing a variable EmBeck87 15 2,812 Apr-17-2023, 06:54 PM
Last Post: EmBeck87
  Unable to request image from FORM Data usman 0 989 Aug-18-2022, 06:23 PM
Last Post: usman
  json api data parsing elvis 0 923 Apr-21-2022, 11:59 PM
Last Post: elvis
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,649 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  I need help parsing through data and creating a database using beautiful soup username369 1 1,705 Sep-22-2021, 08:45 PM
Last Post: Larz60+
  Problem: Retrieving Form data PythonDev 3 3,076 Oct-16-2020, 02:09 AM
Last Post: PythonDev
  Write lambda function in pyhhon to coy data from multiple JSON into a single JSON fil anandmn85 2 4,227 Apr-19-2018, 05:56 AM
Last Post: anandmn85
  how to parse multipart/form-data for xls or jpeg stream into python code and store v71017 0 3,307 Mar-20-2018, 01:09 PM
Last Post: v71017
  Parsing serial data "Attribute Error" Zeberoff 2 3,410 Jan-04-2018, 09:57 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020