Reputation: 43
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)
Upvotes: 1
Views: 595
Reputation: 34551
Deta is very restrictive. As shown in the Technical Specifications, Deta Micros (micro servers) provide a read-only file system, only /tmp
can be written to, which has a 512 MB storage limit.
Deta provides two additional services, i.e., Deta Base (a fully-managed, NoSQL database, which you can use to store, query, update and delete records in the database) and Deta Drive (a scalable file storage service, which you can use to upload and store files).
Deta Drive is the one you should be looking for in your case. However, as per the documentation, Deta Micros have HTTP payload size limit of 5.5 MB, meaning that if you would like to upload a file that is larger than 5.5 MB, you can't—unless you upload the file to your API in chunks of 5.5 MB, and then from your API re-upload the file to Deta Drive, using this endpoint if the file size is less than 10 MB; otherwise, if the file is larger than 10 MB, you would have to upload the file in chunks, using chunked upload. As per Upload Chunked Part:
Each chunk must be at least 5 Mb and at most 10 Mb. The final chunk can be less than 5 Mb.
From the code snippet you provided, you seem to be writing the file to the /tmp
directory, using Path
from pathlib
module. Please try specifying the path without using the pathlib
module, as well as try specifying the directory with ./
at the start, for example:
path = Path("./tmp") / file.filename
You said that you get an Internal Server Error
, which is a very generic error and says little about the nature of the error. You should instead take a look at the actual log from your service (logging
example can be found here), which would tell what is causing the app to fail. If you provided the full traceback, it would really help in identifying the issue.
Upvotes: 1
Reputation: 43
The error occured due to absence of poppler-utils
and has nothing to do with FastApi.
My program requires linux package poppler-utils
to process pdf files. Since Deta cloud does not have one and does not allow users to execute any ... apt install ...
commands, aforementioned error occured.
Upvotes: -1