This is a simple FastAPI application that provides APIs for creating and serving files. The application allows you to create a file with a unique ID and download it. After the file is served for download, it is automatically deleted from the server.
The application uses a directory named files to store the files temporarily. If the directory does not already exist, it is created automatically when the application starts.
This endpoint is used to create a new file with a unique ID.
-
Description:
- A unique file ID is generated using
uuid. - A text file is created in the
filesdirectory with the content:
This is a test file. with file id <file_id>. - The file ID is returned in the response.
- A unique file ID is generated using
-
Response:
- A JSON object containing the unique
file_id.
- A JSON object containing the unique
-
Example Response:
{ "file_id": "123e4567-e89b-12d3-a456-426614174000" }
This endpoint is used to download a file by its unique ID.
-
Description:
- The application looks for a file with the name
<file_id>.txtin thefilesdirectory. - If the file exists, it is served as a downloadable attachment.
- After serving the file, it is deleted from the
filesdirectory. - If the file does not exist, the application returns a
404 File Not Founderror.
- The application looks for a file with the name
-
Response:
- If the file exists, the file is served as an attachment for download.
- If the file does not exist, a
404error is returned.
-
Example Response for Success:
- The file is downloaded as
<file_id>.txt.
- The file is downloaded as
-
Example Response for Failure:
{ "detail": "File not found" }
- The variable
BASE_DIRis aPathobject representing thefilesdirectory. - The directory is created automatically using the
mkdir(exist_ok=True)method.
- A unique file ID is generated using
uuid.uuid4. - A file is created in the
BASE_DIRdirectory with the name<file_id>.txt. - The content of the file includes the text:
This is a test file. with file id <file_id>.
- The file path is constructed as
BASE_DIR / <file_id>.txt. - The application checks if the file exists using
file_path.exists()andfile_path.is_file(). - If the file exists, it is served as a
FileResponsewith the appropriate headers for downloading. - After the file is served, the
unlink()method is called to delete the file. - If an error occurs during deletion, it is logged but does not interrupt the response.
-
Install FastAPI and Uvicorn:
pip install fastapi uvicorn
-
Start the FastAPI server:
uvicorn main:app --reload
-
Access the API documentation at http://127.0.0.1:8000/docs.
- Send a
POSTrequest to/create-file/. - Copy the
file_idfrom the response.
- Send a
GETrequest to/fileserver/{file_id}, replacing{file_id}with the unique file ID. - The file will be downloaded and automatically deleted from the server.
- Ensure the application has write permissions to the
filesdirectory. - Files are deleted automatically after being served for download. If the file is not downloaded, it will remain in the
filesdirectory.