【发布时间】:2020-05-07 14:39:22
【问题描述】:
几个月以来,我一直在通过 AWS Lambda 函数将 FastAPI 与 Serverless 一起使用,并且运行良好。
我正在创建一个新的 api 端点,它需要发送一个文件。
在我的本地机器上使用时效果很好,但是当我部署到 AWS Lambda 时,当我尝试调用我的端点时出现以下错误,并且文件与在本地工作的完全相同。我目前正在通过 swagger UI 进行测试,并且在运行代码的“机器”旁边的无服务器或本地机器之间没有任何变化。
你知道发生了什么吗?
Python 3.8 FastAPI 0.54.1
我的代码:
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
【问题讨论】:
-
你能不能试试
file: bytes = File(...)而不是file: UploadFile = File(...)然后把字节流直接传给读取excel函数 -
不,无论我喜欢用字节还是 UploadFile,这都不会改变,同样的错误
标签: python amazon-web-services aws-lambda serverless fastapi