Question

Permission denied on App Platform with Python FastAPI

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


Submit an answer


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!

Sign In or Sign Up to Answer

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.

Accepted Answer

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:

https://docs.digitalocean.com/products/spaces/reference/s3-sdk-examples/

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.

import tempfile

with tempfile.NamedTemporaryFile(delete=True) as temp:
    temp.write(content)
    # SOME CODE THAT READS THE FILE

Hope that this helps!

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel