我正在尝试将一个大文件 (\xe2\x89\xa53GB) 上传到我的 FastAPI 服务器,而不将整个文件加载到内存中,因为我的服务器只有 2GB 可用内存。
\n服务器端:
\nasync def uploadfiles(upload_file: UploadFile = File(...):\nRun Code Online (Sandbox Code Playgroud)\n客户端:
\nm = 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 )\nRun Code Online (Sandbox Code Playgroud)\n返回:
\nasync def uploadfiles(upload_file: UploadFile = File(...):\nRun Code Online (Sandbox Code Playgroud)\n我不确定MultipartEncoder实际发送到服务器的内容,因此请求不匹配。有任何想法吗?
python file-upload starlette fastapi python-requests-toolbelt
我正在尝试从 FastAPI 后端下载一个大文件 ( .tar.gz)。在服务器端,我只是验证文件路径,然后Starlette.FileResponse返回整个文件\xe2\x80\x94,就像我在 StackOverflow 上的许多相关问题中看到的那样。
服务器端:
\nreturn FileResponse(path=file_name, media_type=\'application/octet-stream\', filename=file_name)\nRun 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\nRun Code Online (Sandbox Code Playgroud)\n我也尝试使用StreamingResponse,但得到了同样的错误。还有其他方法可以做到吗?
我的代码中的StreamingResponse:
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)