article

Sunday, February 5, 2023

FastAPI Upload Image

FastAPI Upload Image

Install Fastapi

https://github.com/tiangolo/fastapi

pip install fastapi
C:\fastAPI\upload>pip install fastapi
C:\fastAPI\upload>pip install "uvicorn[standard]"

Create main.py
upload/main.py
 
#upload/main.py
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import FileResponse
import os
from random import randint
import uuid

IMAGEDIR = "images/"

app = FastAPI()


@app.post("/upload/")
async def create_upload_file(file: UploadFile = File(...)):

    file.filename = f"{uuid.uuid4()}.jpg"
    contents = await file.read()

    #save the file
    with open(f"{IMAGEDIR}{file.filename}", "wb") as f:
        f.write(contents)

    return {"filename": file.filename}


@app.get("/show/")
async def read_random_file():

    # get random file from the image directory
    files = os.listdir(IMAGEDIR)
    random_index = randint(0, len(files) - 1)

    path = f"{IMAGEDIR}{files[random_index]}"
    
    return FileResponse(path)
run the FastAPI app

C:\fastAPI\upload>uvicorn main:app --reload

with uvicorn using the file_name:app_instance
open the link on the browser http://127.0.0.1:8000/
http://127.0.0.1:8000/docs

Related Post