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
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #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) |
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