I'm working with Python 3.8 on AWS Lambda, and I would like to handle posted file, like requests.files with Django. But it's impossible with AWS Lambda. I want to put this file in the S3 with :
s3.put_object(
Body=fileAsString,
Bucket=MY_BUCKET,
Key='my-file.jpg'
)
When I return directly the received event, with the file :
First try : with cgi.parse_multipart :
I my handler :
c_type, c_data = parse_header(event['headers']['Content-Type'])
boundary = c_data['boundary'].encode('latin1')
body = event['body'].encode('utf8')
fp = BytesIO(body)
pdict = {
'boundary': boundary,
'CONTENT-LENGTH': str(len(body))
}
form_data = parse_multipart(fp, pdict)
fileBytes: bytes = form_data['file'][0]
return ({'statusCode': 200, 'body': json.dumps(str(fileBytes))}
I receive :
I also tried with form_data['file'][0].decode('utf-8') but I receive :
and I have always the "?"
I should get this, because it's the original image, opened in edition :

Second try : I follow this tutorial : https://medium.com/swlh/upload-binary-files-to-s3-using-aws-api-gateway-with-aws-lambda-2b4ba8c70b8e
So, I tried :
file_content = base64.b64decode(event['body'])
but I get :
string argument should contain only ASCII characters
(the same with validate=False)
and when I try to load body as dict with :
body2 = json.loads(event['body'])
I get :
loads failed : Expecting value: line 1 column 1 (char 0)
I tried to add "image/png" and "image/*" to "Binary Media Types" in the settings of the API, but no change.
Any idea ?

