You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure部署的Python-Flask聊天Bot如何无存储生成文件下载链接?

Absolutely! You can totally serve temporary download links directly from your Flask app hosted on Azure without setting up any Azure Storage services—this is perfect for your tiny, short-lived files. Here’s a step-by-step breakdown of how to make it work:

1. Pick a Temporary Storage Spot

Since your files are only 10-20 kB and don’t need long-term retention, use Azure App Service’s local temporary storage or a dedicated temp folder in your app directory:

  • Azure provides a system temp folder accessible via os.environ.get('TEMP') (Windows) or /tmp (Linux). This storage gets wiped when your app restarts, which aligns perfectly with your "auto-delete after timeout" needs.
  • Alternatively, create a temp_files folder in your Flask app’s root. Just make sure to add cleanup logic to remove old files later.

2. Add a Download Route to Your Flask App

Create a dedicated route that serves the temp files to users, with safeguards to prevent common security risks like path traversal attacks. Here’s a simple example:

from flask import Flask, send_file, abort
import os
import uuid
from werkzeug.utils import safe_join

app = Flask(__name__)
# Define your temp folder (adjust based on your Azure environment)
TEMP_FOLDER = os.path.join(app.root_path, 'temp_files')
os.makedirs(TEMP_FOLDER, exist_ok=True)

@app.route('/download/<file_id>')
def download_file(file_id):
    # Prevent path traversal by safely joining paths
    file_path = safe_join(TEMP_FOLDER, f"{file_id}.txt") # Adjust extension as needed
    
    # Check if the file exists (add expiry check here if you want)
    if not os.path.exists(file_path):
        abort(404, description="File not found or has expired")
    
    # Optional: Delete the file right after download since it's temporary
    try:
        return send_file(file_path, as_attachment=True, download_name="your-file.txt")
    finally:
        os.remove(file_path)

When your chat bot processes a user’s request and creates the file:

  1. Generate a unique file ID (using uuid.uuid4() is a safe way to avoid collisions).
  2. Save the file to your temp folder with that unique ID as the filename (e.g., abc123-def456.txt).
  3. Create a download link using your app’s public URL + the route, like https://your-app-name.azurewebsites.net/download/abc123-def456.
  4. Send this link directly to the user in the chat.

4. Add Automatic Cleanup (Fallback)

Even if you delete files after download, it’s smart to have a backup plan for files users never download:

  • Use a background scheduler like APScheduler to run a task every hour that deletes files older than your timeout (e.g., 2 hours).
  • Add cleanup logic when your app starts to clear any leftover files from previous runs.

Quick Azure App Service Notes

  • Local temp storage has size limits (usually a few GB), but your tiny files won’t come close to hitting that.
  • If your app runs on multiple instances, each instance has its own local storage. Since your files are per-user and short-lived, this isn’t a problem—just generate the file on the instance handling the user’s request, and send the link tied to that instance (sticky sessions can help here if needed).

内容的提问来源于stack exchange,提问作者Vijay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:21:00