我已经通过 AWS Lambda 函数将 FastAPI 与无服务器结合使用几个月了,效果非常好。
我正在创建一个新的 api 端点,它需要发送一个文件。
在我的本地计算机上使用时它工作得很好,但是当我部署到 AWS Lambda 时,当我尝试使用与本地工作完全相同的文件调用我的端点时,出现以下错误。我目前正在通过 swagger UI 进行测试,除了运行代码的“机器”之外,我的无服务器或本地机器之间没有任何变化。
你知道发生了什么事吗?
Python 3.8
快速API 0.54.1
My code:
from fastapi import FastAPI, File, UploadFile
import pandas as pd
app = FastAPI()
@app.post('/process_data_import_quote_file')
def process_data_import_quote_file(file: UploadFile = File(...)): # same error if I put bytes instead of UploadFile
file = file.file.read()
print(f"file {file}")
quote_number = pd.read_excel(file, sheet_name='Data').iloc[:, 0].dropna()
最后一行失败
我尝试打印该文件,当我将打印的数据与本地读取的数据进行比较时,发现有所不同。我发誓这与我在 2 上使用的文件相同,所以我不知道什么可以解释这一点?
这是一个非常基本的 Excel 文件,没有什么特别的。
[ERROR] 2020-05-07T14:25:17.878Z 25ff37a5-e313-4db5-8763-1227e8244457 Exception in ASGI application
Traceback (most recent call last):
File "/var/task/mangum/protocols/http.py", line 39, in run
await app(self.scope, self.receive, self.send)
File "/var/task/fastapi/applications.py", line 149, in __call__
await super().__call__(scope, receive, send)
File "/var/task/starlette/applications.py", line 102, in __call__
await self.middleware_stack(scope, receive, send)
File "/var/task/starlette/middleware/errors.py", line 181, in __call__
raise exc from None
File "/var/task/starlette/middleware/errors.py", line 159, in __call__
await self.app(scope, receive, _send)
File "/var/task/starlette/exceptions.py", line 82, in __call__
raise exc from None
File "/var/task/starlette/exceptions.py", line 71, in __call__
await self.app(scope, receive, sender)
File "/var/task/starlette/routing.py", line 550, in __call__
await route.handle(scope, receive, send)
File "/var/task/starlette/routing.py", line 227, in handle
await self.app(scope, receive, send)
File "/var/task/starlette/routing.py", line 41, in app
response = await func(request)
File "/var/task/fastapi/routing.py", line 196, in app
raw_response = await run_endpoint_function(
File "/var/task/fastapi/routing.py", line 150, in run_endpoint_function
return await run_in_threadpool(dependant.call, **values)
File "/var/task/starlette/concurrency.py", line 34, in run_in_threadpool
return await loop.run_in_executor(None, func, *args)
File "/var/lang/lib/python3.8/concurrent/futures/thread.py", line 57, in run
result = self.fn(*self.args, **self.kwargs)
File "/var/task/app/quote/processing.py", line 100, in process_data_import_quote_file
quote_number = pd.read_excel(file, sheet_name='Data').iloc[:, 0].dropna()
File "/var/task/pandas/io/excel/_base.py", line 304, in read_excel
io = ExcelFile(io, engine=engine)
File "/var/task/pandas/io/excel/_base.py", line 821, in __init__
self._reader = self._engines[engine](self._io)
File "/var/task/pandas/io/excel/_xlrd.py", line 21, in __init__
super().__init__(filepath_or_buffer)
File "/var/task/pandas/io/excel/_base.py", line 355, in __init__
self.book = self.load_workbook(BytesIO(filepath_or_buffer))
File "/var/task/pandas/io/excel/_xlrd.py", line 34, in load_workbook
return open_workbook(file_contents=data)
File "/var/task/xlrd/__init__.py", line 115, in open_workbook
zf = zipfile.ZipFile(timemachine.BYTES_IO(file_contents))
File "/var/lang/lib/python3.8/zipfile.py", line 1269, in __init__
self._RealGetContents()
File "/var/lang/lib/python3.8/zipfile.py", line 1354, in _RealGetContents
fp.seek(self.start_dir, 0)
ValueError: negative seek value -62703616