[Python] Upload File operation using FastAPI does not work on Deta Cloud Platform

Discussão em 'Python' iniciado por Stack, Março 1, 2025.

  1. Stack

    Stack Membro Participativo

    When I run this code via uvicorn main:app everything works, but when I run it using Deta, I get Internal Server Error and only GET endpoint works (the POST one is not even called as it was supposed to). Can you please help me? Thanks in advance.

    import os
    from pathlib import Path

    from fastapi import FastAPI, Form, UploadFile
    from fastapi.responses import FileResponse, HTMLResponse

    from pdfpad import pdfpad, save_pdf


    app = FastAPI()


    @app.post("/processfile/")
    async def process_file(file: UploadFile) -> FileResponse:
    path = file.filename
    if os.getenv("DETA_RUNTIME") == "true":
    path = Path("/tmp") / file.filename
    saved_path = save_pdf(pdfpad(await file.read()), path)
    return FileResponse(saved_path)


    @app.get("/")
    async def main():
    content = """
    <body>
    <form action="/processfile/" enctype="multipart/form-data" method="post" id="form1">

    <label for file>Choose PDF:</label>
    file: <input name="file" type="file" form="form1">
    <br><br>

    <input type="submit">
    </form>
    </body>
    """
    return HTMLResponse(content=content)

    Continue reading...

Compartilhe esta Página