Search code examples
djangoherokudjango-modelsamazon-s3heroku-postgres

Read and write a DB file stored in aws s3 (Heroku + Django)


I´m stuck with an easy requirement, I appreciate any ideas!

First the enviroment: Django app deployed on Heroku with a Postgres DB and serving the static files from AWS S3.

I have a model with a FileField, and when I upload a file it is stored in a AWS S3 bucket.

I just need to read the file, write some text and save it again, it should be easy, right?

I managed to do it in dev in my laptop, but when I deploy the app to heroku I get a lot of errors like a file path error (I guess it tries to look for the aws bucket path in the heroku server), write permmissions and some others...

Am I doing something wrong? How can I achieve this? Thx in advanced!


Solution

  • I was thinking wrong...

    The solutions is to read and write directly in the defined storage:

    from django.core.files.storage import default_storage
    with default_storage.open(model.field.name, "w+") as f:
        f.write("hello")
    

    Hope this can be useful to anyone