gpt4 book ai didi

Python3.8 - FastAPI 和无服务器 (AWS Lambda) - 无法处理发送到 api 端点的文件

转载 作者:行者123 更新时间:2023-12-04 02:36:23 26 4
gpt4 key购买 nike

几个月来,我一直通过 AWS Lambda 函数将 FastAPI 与 Serverless 结合使用,并且效果很好。

我正在创建一个新的 api 端点,它需要发送一个文件。

在我的本地机器上使用时它可以完美运行,但是当我部署到 AWS Lambda 时,当我尝试使用与本地工作完全相同的文件调用我的端点时出现以下错误。我目前正在通过 swagger UI 进行此测试,除了运行代码的“机器”之外,我的无服务器或本地机器之间没有任何变化。

你知道发生了什么吗?

python 3.8
快速API 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

最佳答案

这是由于 AWS API Gateway。
我不得不继续在 API Gateway 中允许多部分/表单数据
并使用 file = BytesIO(file).read() 进行更正能够正确使用文件流。

关于Python3.8 - FastAPI 和无服务器 (AWS Lambda) - 无法处理发送到 api 端点的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61660586/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com