Skip to content

sanjaysagar12/FastAPI-File-Server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

FastAPI File Server

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.

How It Works

Directory Setup

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.

Endpoints

1. POST /create-file/

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 files directory with the content:
      This is a test file. with file id <file_id>.
    • The file ID is returned in the response.
  • Response:

    • A JSON object containing the unique file_id.
  • Example Response:

    {
        "file_id": "123e4567-e89b-12d3-a456-426614174000"
    }

2. GET /fileserver/{file_id}

This endpoint is used to download a file by its unique ID.

  • Description:

    • The application looks for a file with the name <file_id>.txt in the files directory.
    • If the file exists, it is served as a downloadable attachment.
    • After serving the file, it is deleted from the files directory.
    • If the file does not exist, the application returns a 404 File Not Found error.
  • Response:

    • If the file exists, the file is served as an attachment for download.
    • If the file does not exist, a 404 error is returned.
  • Example Response for Success:

    • The file is downloaded as <file_id>.txt.
  • Example Response for Failure:

    {
        "detail": "File not found"
    }

Code Walkthrough

BASE_DIR

  • The variable BASE_DIR is a Path object representing the files directory.
  • The directory is created automatically using the mkdir(exist_ok=True) method.

POST /create-file/

  • A unique file ID is generated using uuid.uuid4.
  • A file is created in the BASE_DIR directory with the name <file_id>.txt.
  • The content of the file includes the text: This is a test file. with file id <file_id>.

GET /fileserver/{file_id}

  • The file path is constructed as BASE_DIR / <file_id>.txt.
  • The application checks if the file exists using file_path.exists() and file_path.is_file().
  • If the file exists, it is served as a FileResponse with 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.

Running the Application

  1. Install FastAPI and Uvicorn:

    pip install fastapi uvicorn
  2. Start the FastAPI server:

    uvicorn main:app --reload
  3. Access the API documentation at http://127.0.0.1:8000/docs.

Example Usage

Create a File

  1. Send a POST request to /create-file/.
  2. Copy the file_id from the response.

Download a File

  1. Send a GET request to /fileserver/{file_id}, replacing {file_id} with the unique file ID.
  2. The file will be downloaded and automatically deleted from the server.

Notes

  • Ensure the application has write permissions to the files directory.
  • Files are deleted automatically after being served for download. If the file is not downloaded, it will remain in the files directory.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages