[英]How to receive URL File as parameter and save it to disk using FastAPI?
I would like to know if there is a way for FastAPI to receive a URL of a file as a parameter and save this file to disk?我想知道是否有办法让 FastAPI 接收一个文件的 URL 作为参数并将这个文件保存到磁盘? I know it is possible with the
requests
library using requests.get()
method, but is it possible with FastAPI to receive the URL and save it directly?我知道
requests
库可以使用requests.get()
方法,但是 FastAPI 可以接收 URL 并直接保存吗?
I tried using file: UploadFile = File(...)
, but then it doesn't download the file when the URL is sent.我尝试使用
file: UploadFile = File(...)
,但是当发送 URL 时它不会下载文件。
I don't believe so.我不这么认为。 I've come across this before and was unable to find a solution (and ended up using requests like you mentioned), but seeing this I wanted to check again more thoroughly.
我以前遇到过这个问题,但找不到解决方案(最终使用了你提到的请求),但看到这个我想更彻底地再次检查。
Reviewing the uvicorn
and fastapi
repositories by searching the code itself, I see no functions/code that reference requests
or urllib
(they do use urllib.parse
/ quote
, etc though) that would be 2 likely suspects to build requests.通过搜索代码本身来查看
uvicorn
和fastapi
存储库,我没有看到任何函数/代码引用requests
或urllib
(尽管它们确实使用urllib.parse
/ quote
等),这两个可能是构建请求的嫌疑人。 They do use httpx.AsyncClient
, but only in tests.他们确实使用
httpx.AsyncClient
,但仅在测试中使用。 I would expect to see some use of these libraries in the main uvicorn
/ fastapi
libraries if they had code to make external requests.我希望在主要的
uvicorn
/ fastapi
库中看到这些库的一些使用,如果它们有代码来发出外部请求的话。
Seeing the above, I actually think I will change my code to use httpx.AsyncClient
anyways since it is already a dependency.看到上面的内容,我实际上认为我会更改我的代码以无论如何使用
httpx.AsyncClient
,因为它已经是一个依赖项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.