Search code examples
herokuelixirautomatic-ref-countingplug

Can't load images users uploaded with arc on prod on heroku


I use arc to save users uploads and add to the db. On localhost everything works fine with (in lib/app_web/endpoint.ex)

plug Plug.Static,
    at: "/uploads", from: Path.expand("./uploads"), gzip: false

I can't understand where I can save uploads on heroku so I can access them with https://app.herokuapp.com/uploads/images/...


Solution

  • The thing with uploading files direct to Heroku is that they get deleted every time your dyno stops/restarts. So you need to store those files outside of your Heroku application. One solution is to use AWS S3 (it's quite cheap if you don't have lots of data, like half dollar a month if you store 20 GB and have 100GB of traffic).

    From the Heroku help pages:

    The Heroku filesystem is ephemeral - that means that any changes to the filesystem whilst the dyno is running only last until that dyno is shut down or restarted. Each dyno boots with a clean copy of the filesystem from the most recent deploy. This is similar to how many container based systems, such as Docker, operate.

    In addition, under normal operations dynos will restart every day in a process known as "Cycling".

    These two facts mean that the filesystem on Heroku is not suitable for persistent storage of data. In cases where you need to store data we recommend using a database addon such as Postgres (for data) or a dedicated file storage service such as AWS S3 (for static files). If you don't want to set up an account with AWS to create an S3 bucket we also have addons here that handle storage and processing of static assets https://elements.heroku.com/addons