В этом примере я покажу вам, как загрузить, загрузить, удалить и получить файлы с помощью fastapi
Как загружать файлы по формам данных, используя fastapi
В следующем коде мы определяем поле файла, именно там мы получим файл по формам формы
from fastapi import FastAPI, UploadFile, File app = FastAPI() @app.post("/upload") async def upload_file(file: UploadFile = File(...)): with open(file.filename, 'wb') as image: content = await file.read() image.write(content) image.close() return JSONResponse(content={"filename": file.filename}, status_code=200)
Как скачать файлы с помощью fastapi
from fastapi import FastAPI from os import getcwd from fastapi.responses import FileResponse app = FastAPI() @router.get("/download/{name_file}") def download_file(name_file: str): return FileResponse(path=getcwd() + "/" + name_file, media_type='application/octet-stream', filename=name_file)
Как получить файлы с помощью fastapi
from fastapi import FastAPI from os import getcwd from fastapi.responses import FileResponse app = FastAPI() @router.get("/file/{name_file}") def get_file(name_file: str): return FileResponse(path=getcwd() + "/" + name_file)
Как удалить файлы с помощью fastapi
from fastapi import FastAPI from os import getcwd, remove from fastapi.responses import JSONResponse app = FastAPI() @router.delete("/delete/file/{name_file}") def delete_file(name_file: str): try: remove(getcwd() + "/" + name_file) return JSONResponse(content={ "removed": True }, status_code=200) except FileNotFoundError: return JSONResponse(content={ "removed": False, "error_message": "File not found" }, status_code=404)
Оригинал: “https://dev.to/nelsoncode/how-to-create-server-of-files-with-fastapi-47d0”