WEBVTT

ï»¿1
00:00:00.917 --> 00:00:07.424
Today. we will learn how to serve static files. particularly image files. using FastAPI.  

00:00:08.633 --> 00:00:16.683
The example we will use assumes that a file named `main.py` and a folder named `log` are in the same directory level.  

00:00:18.476 --> 00:00:24.024
The `log` folder contains image files that are configured for external access. 

00:00:25.275 --> 00:00:27.235
Now. let's take a look at the code.  

00:00:28.445 --> 00:00:30.613
First. we import FastAPI and StaticFiles. 

00:00:31.281 --> 00:00:36.161
StaticFiles is a module used in FastAPI for serving static files.  

00:00:36.828 --> 00:00:40.540
We will also use the os module to set file paths.  

00:00:43.001 --> 00:00:48.423
Now. we set the path to serve the `log` folder as a static file directory.  

00:00:49.174 --> 00:00:58.641
We use `os.path.join` and `os.path.dirname` to set the path for the `log` folder. which is at the same directory level as `main.py`.  

00:00:59.601 --> 00:01:03.521
Finally. we mount the `log` folder using StaticFiles.  

00:01:04.689 --> 00:01:12.238
This allows access to an image file by appending `/log/image-filename` to the server address.  

00:01:13.031 --> 00:01:20.705
Now. when we run the FastAPI server. external access to image files in the `log` folder becomes possible. 

00:01:21.372 --> 00:01:25.168
It can be accessed by appending the filename to /log/.

00:01:25.877 --> 00:01:30.048
However. in real-world applications. security is crucial.  

00:01:30.799 --> 00:01:35.720
Typically. when serving images using cloud services like AWS.  

00:01:35.720 --> 00:01:39.224
Access is restricted through security configurations.  

00:01:40.558 --> 00:01:45.605
For instance. access may be restricted to specific IP addresses or domains.  

00:01:46.022 --> 00:01:50.777
Alternatively. access may be limited to authenticated users only.  

00:01:51.236 --> 00:01:55.824
Such security measures prevent unauthorized access to data.  

00:01:55.824 --> 00:01:59.202
And are essential for ensuring service stability.  

00:01:59.911 --> 00:02:08.253
In this way. we have explored how to serve static files using FastAPI and the importance of security configurations. 

00:02:08.878 --> 00:02:15.510
Using this method. you can easily serve images and other static files in your web application.  
