Store file in directory tmp on heroku Rails
Asked Answered
C

1

17

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

Consignor answered 31/8, 2013 at 21:19 Comment(0)
B
24

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."
Broadloom answered 31/8, 2013 at 21:38 Comment(1)
Thank you, I will store in s3Consignor

© 2022 - 2024 — McMap. All rights reserved.