DEV Community

Dang Hoang Nhu Nguyen
Dang Hoang Nhu Nguyen

Posted on

[BTY] Day 2: Make image's link from the server's hard drive via FAST API

When I think about "image link", I only know about a link generated after uploading an image to a hosting service. But thank @vietnhh, now I have found another solution: make an image link from the server's hard drive via FAST API.

...
@router.get("/get-image", tags=["get image from disk"])
async def get_image_from_disk(image_path: str):
    image_name = os.path.basename(image_path)
    _, ext = os.path.splitext(image_name)

    if os.path.exists(image_path):
        return FileResponse(image_path, media_type=f"image/{ext}", filename=image_name)
    return {"error": "File not found!"}

Enter fullscreen mode Exit fullscreen mode

And the image's link will be something link: http://your_ip:your_port/get-image/mnt/data/images/vietnhh.jpg

Any disadvantages?

Top comments (0)