Skip to main content

A FastAPI web file uploader

Project description

Web File Uploader

一個使用 FastAPI 建構的檔案上傳與限時下載服務,下載路徑含 UUID 前綴、到期自動清除。

安裝 uv

如果尚未安裝 uv,請先安裝:

# 使用 pip 安裝 uv
pip install uv

# 或使用官方安裝腳本
curl -LsSf https://astral.sh/uv/install.sh | sh

開發和除錯

使用虛擬環境開發

  1. 安裝依賴到現有的虛擬環境:

    cd web-file-uploader
    & ..\venv\Scripts\Activate.ps1
    # 若 uv 在 Windows 碰到 editable 安裝問題,改用 pip 並關閉隔離
    python -m pip install -e .[dev] --no-build-isolation --config-settings editable_mode=compat
    # 或 uv:uv pip install -e .[dev] --no-build-isolation --config-settings editable_mode=compat
    
  2. 啟動開發伺服器(支援自動重新載入):

    uvicorn main:app --reload --host 0.0.0.0 --port 8000
    

使用 uvx 部署

在專案目錄中執行:

uvx --from . web-file-uploader

這將安裝依賴並啟動 FastAPI 伺服器在 http://localhost:8000

API 端點(符合 file-expiry 計畫)

  • GET /: 歡迎訊息和 API 文檔鏈接
  • POST /upload: 上傳檔案,伺服器統一設定有效期,建立紀錄並回傳下載 URL:/download/{uuid}/{filename}
  • GET /download/{uuid}/{filename}: 依 UUID+檔名驗證,未過期且檔案存在才回傳,否則 404 並清理紀錄/檔案
  • GET /files: 列出未過期檔案(含下載 URL、到期時間)
  • DELETE /files/{filename}: 刪除指定檔案

設定與行為

  • 預設有效期:60 秒,可由 --expire-seconds 或環境變數 EXPIRE_SECONDS 調整(範圍 10–86400,超界會 clamp)
  • 清理排程:預設每 300 秒執行一次,可用 --cleanup-interval-secondsCLEANUP_INTERVAL_SECONDS(範圍 30–3600)
  • 即時清理:下載時若發現過期或檔案缺失即刪除紀錄/檔案
  • 持久化:SQLite(自動建立 file_records.db),重啟後仍可判斷到期

測試

  • 執行整合測試:
    python -m pytest tests/integration/test_file_expiry.py -q
    # 或全部測試
    python -m pytest -q
    

API 文檔

啟動伺服器後,訪問 http://localhost:8000/docs 查看完整的互動式 API 文檔。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

web_file_uploader-0.2.0.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

web_file_uploader-0.2.0-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file web_file_uploader-0.2.0.tar.gz.

File metadata

  • Download URL: web_file_uploader-0.2.0.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for web_file_uploader-0.2.0.tar.gz
Algorithm Hash digest
SHA256 f764841b0e02c31003df3fccb4e15d8e207ea0b1e4c634f13f5135224ecfeff1
MD5 2d84c2bc121ca8cc1774d9f5a21f4193
BLAKE2b-256 696fc01c8c140403ef277db3346dba12f0560e91d71ba7ab77a803db18951437

See more details on using hashes here.

File details

Details for the file web_file_uploader-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: web_file_uploader-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 9.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for web_file_uploader-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1e688f574a22873913a431aa852b53fde7b49f795233e5f52f85b792871824a3
MD5 e3144ff148933ccc925a1feea57a6e7d
BLAKE2b-256 c1a2ad8bbbca2932212367e9d7389cd38fde5c8e4f0ed615ce76bb9b7a4fcec8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page