After git push heroku - uploaded files on Heroku are lost

My fairly basic application allows users to upload avatars.

The application is deployed to Heroku with

$ git add .
$ git commit -m "description"
$ git checkout master
$ git merge my-cool-new-feature
$ git push heroku

The problem is, every time I push changes to Heroku, all files uploaded to Heroku are lost. I thought, the problem was that the folder/files were under version control, so I added the folder to .gitignore

# Ignore User generated files
/public/system/*

and removed the files from the repository.

$ git rm -rf --cached public/system

But the problem persists. Can you point me in the right direction?


Solution 1:

The reason for this is because Heroku limits the way you can store data on their servers. Back in the bamboo stack days, storing any data was simply impossible without the use of an external service. Since they introduced the Cedar stack, things have changed a little bit, but storing persistent data is still not possible.

As you've discovered, each time you push a new change to your Heroku application (or each time the application shuts down and restarts after being inactive for x minutes), your application is recreated and all stored data is lost.

Your best bet is to not use the /public directory at all, and start using an external service like Amazon S3, Rackspace Cloud Files or Spideroak.

Solution 2:

If your application needs to receive files uploaded by users, you need to make sure these uploads are stored in a central and durable location.

With Heroku’s ephemeral filesystem, any information written to a dyno’s filesystem will be lost when the dyno is restarted. Instead, Heroku recommends backing services. For file and media storage, Amazon’s Simple Storage Service (S3) is a great solution.

You might want to read this arcticle on Heroku website: Uploading Files to S3 in Ruby with Paperclip

Solution 3:

Your files will be lost with every deployment. My preferred solution is using Paperclip and an Amazon bucket. Paperclip will store your image in the bucket so it can be referenced at will from the app.