Store file in directory tmp on heroku Rails

ZeroX  picture ZeroX · Aug 31, 2013 · Viewed 13.8k times · Source

In my Delayed Job, I tried to create a file to tmp folder

file_path = Rails.root.join('tmp', "#{file_name}." + file_extension);
exported_file = kit.to_pdf
# Save file to disk
File.open(file_path, 'wb') do |file|
      file << exported_file 
end

It works well in local but on Heroku there is a error in Delayed Job "No such file or directory - /app/tmp/test.pdf"

So how I can solve this problem. I do not want to store file in S3. Thank you

Answer

Benjamin Manns picture Benjamin Manns · Aug 31, 2013

Heroku uses what is called an ephemeral filesystem. This means that your local filesystem is only accessible to a single dyno, and once the dyno is stopped, restarted, or moved, all files on the local filesystem are destroyed.

The only way for your Delayed Job process to transfer files to an outside process would be to store the files in a more permanent location. This could be S3, a database, Rackspace Files, etc, but for Heroku it cannot be the local filesystem.

However, if you are just looking to store the file in a temporary scratch location, it is fine to use the local filesystem. It looks like you may be having issues because the /app/tmp directory may not exist.

Try adding this to your worker:

Dir.mkdir(Rails.root.join('tmp'))

Or, add the tmp directory to your git repository.

mkdir tmp
touch tmp/.keep
git add tmp/.keep
git commit -m "Add tmp directory to app repository."