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?
Heroku does not support file uploads. The filesystem is readonly. You'll have to host your uploaded files somewhere else (or from the database, which is a bad option). If you are using any gems like paperclip or carrierwave for uploading, using S3 will be simple.
Log onto your heroku instance using heroku run bash. Use base64 to print the contents of your file: base64 <your-file> Select the base64 text in your terminal and copy it. On your local machine decompress this text using base64 straight into a new file (on a mac I'd do pbpaste | base64 --decode -o <your-file> )
Files are uploaded directly to the cloud from your user's browser, without passing through your application. Adding direct uploads to your app allows you to offload the storage of static files from your app. This is crucial on Heroku, because your app's dynos have an ephemeral filesystem.
Heroku has an “ephemeral” hard drive, this means that you can write files to disk, but those files will not persist after the application is restarted. By default Active Storage uses a :local storage option, which uses the local file system to store any uploaded files.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With