小编Yuq*_*ang的帖子

如何上传大文件(?3GB)到FastAPI后端?

我正在尝试将一个大文件 (\xe2\x89\xa53GB) 上传到我的 FastAPI 服务器,而不将整个文件加载到内存中,因为我的服务器只有 2GB 可用内存。

\n

服务器端

\n
async def uploadfiles(upload_file: UploadFile = File(...):\n
Run Code Online (Sandbox Code Playgroud)\n

客户端

\n
m = MultipartEncoder(fields = {"upload_file":open(file_name,\'rb\')})\nprefix = "http://xxx:5000"\nurl = "{}/v1/uploadfiles".format(prefix)\ntry:\n    req = requests.post(\n    url,\n    data=m,\n    verify=False,\n            )\n
Run Code Online (Sandbox Code Playgroud)\n

返回:

\n
async def uploadfiles(upload_file: UploadFile = File(...):\n
Run Code Online (Sandbox Code Playgroud)\n

我不确定MultipartEncoder实际发送到服务器的内容,因此请求不匹配。有任何想法吗?

\n

python file-upload starlette fastapi python-requests-toolbelt

11
推荐指数
1
解决办法
2万
查看次数

如何使用FastAPI下载大文件?

我正在尝试从 FastAPI 后端下载一个大文件 ( .tar.gz)。在服务器端,我只是验证文件路径,然后Starlette.FileResponse返回整个文件\xe2\x80\x94,就像我在 StackOverflow 上的许多相关问题中看到的那样。

\n

服务器端:

\n
return FileResponse(path=file_name, media_type=\'application/octet-stream\', filename=file_name)\n
Run Code Online (Sandbox Code Playgroud)\n

之后,我收到以下错误:

\n
  File "/usr/local/lib/python3.10/dist-packages/fastapi/routing.py", line 149, in serialize_response\n    return jsonable_encoder(response_content)\n  File "/usr/local/lib/python3.10/dist-packages/fastapi/encoders.py", line 130, in jsonable_encoder\n    return ENCODERS_BY_TYPE[type(obj)](obj)\n  File "pydantic/json.py", line 52, in pydantic.json.lambda\nUnicodeDecodeError: \'utf-8\' codec can\'t decode byte 0x8b in position 1: invalid start byte\n
Run Code Online (Sandbox Code Playgroud)\n

我也尝试使用StreamingResponse,但得到了同样的错误。还有其他方法可以做到吗?

\n

我的代码中的StreamingResponse

\n
  File "/usr/local/lib/python3.10/dist-packages/fastapi/routing.py", line 149, in serialize_response\n    return jsonable_encoder(response_content)\n  File "/usr/local/lib/python3.10/dist-packages/fastapi/encoders.py", line 130, in jsonable_encoder\n    return ENCODERS_BY_TYPE[type(obj)](obj)\n  File …
Run Code Online (Sandbox Code Playgroud)

python download starlette pydantic fastapi

5
推荐指数
1
解决办法
7604
查看次数