FastAPI文件FileResponse对外http下载服务器,python
import os.path
from pathlib import Path
import uvicorn
from fastapi import FastAPI
from starlette.responses import FileResponse
app = FastAPI(name='file_server')
@app.get("/file/{file_name}")
async def download(file_name: str):
# 当前目录作为文件系统根目录
f = os.path.join('./', file_name)
print(Path(f).name)
fr = FileResponse(
path=f,
filename=Path(f).name,
)
return fr
if __name__ == '__main__':
uvicorn.run(app=app, host="0.0.0.0", debug=True)