Python3.8 – FastAPI and Serverless (AWS Lambda) – Unable to process files sent to api endpoint


I’ve been using FastAPI with Serverless through AWS Lambda functions for a couple of months now and it works perfectly.

I’m creating a new api endpoint which requires one file to be sent.

It works perfectly when using on my local machine, but when I deploy to AWS Lambda, I have the following error when I try to call my endpoint, with the exact same file that is working locally. I’m doing this at the moment as a test through the swagger UI and nothing changes between my serverless or my local machine beside the “machine” the code is run on.

Would you have any idea what is going on ?

Python 3.8
FastAPI 0.54.1

My code:

It fails on the last line

I’ve tried to print the file, when I compare the data printed with what I read locally, it is different. I swear it’s the same file I’m using on the 2 so I don’t know what could explain that ?
It’s a very basic excel file, nothing special about it.


It was due to AWS API Gateway.

I have had to proceed allowing multipart/form-data in API Gateway
and correct with file = BytesIO(file).read() to be able to use properly the file stream.

Leave a Reply