I have deployed a FastAPI application in Digitalocen App Platform and I’m trying to upload, write, and modify a file. This is the code I’m using:
@router.post(
"/",
response_model=schemas.GeneralResponseObj,
status_code=status.HTTP_201_CREATED
)
async def load_file(file: UploadFile = File(...), source: str = ''):
"""
Load a file
"""
content = file.file.read()
with open(file.filename, "wb") as f:
f.write(content)
# SOME CODE THAT READS THE FILE
os.remove(file.filename)
return schemas.GeneralResponseObj(
message="Saved new data"
)
It works locally but on the App Platform I receive:
with open(file.filename, "wb") as f:
******* PermissionError: [Errno 13] Permission denied
How can I fix this error? Any help is much appreciated. Thanks
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Hi there,
As the DigitalOcean App Platform storage is ephemeral, meaning that all of the files that you’ve stored on the local storage will be lost during the next deployment, it is best to use an S3 storage like the DigitalOcean Spaces.
So instead of saving files to the local file system, consider saving them to a cloud storage solution like DigitalOcean Spaces. This way, you can handle file uploads and downloads without worrying about the limitations of the local file system. There are libraries like
boto3
for S3 Python client for DigitalOcean Spaces that can help with this:The error you’re seeing is due to the fact that the DigitalOcean App Platform’s file system is read-only for application code. This means you can’t write to or modify files directly on the file system.
If you want to still give this a try with the local file system, you could try to instead of writing to the file system directly, you can use Python’s built-in
tempfile
module to create temporary files. These files will be automatically deleted when they are closed or when the program ends.Hope that this helps!
Best,
Bobby