Heroku - how to write into “tmp” directory?

后端 未结 2 1833
闹比i
闹比i 2021-01-01 21:57

I need to use the tmp folder on Heroku (Cedar) for writing some temporarily data, I am trying to do that this way:

open(\"#{Rails.root}/tmp/#{re         


        
相关标签:
2条回答
  • 2021-01-01 22:25

    Is /tmp included in your git repo? Removed in your .slugignore? The directory may just not exist out on Heroku.

    Try tossing in a quick mkdir before the write:

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

    Or even in an initializer or something...

    0 讨论(0)
  • Here's an elegant way

    f = File.new("tmp/filename.txt", 'w')
    f << "hi there"
    f.close
    
    Dir.entries(Dir.pwd.to_s + ("/tmp")) # See your newly created file in /tmp
    

    Don't forget that whenever your app restarts (for any reason, including those outside your control), your files will be deleted, as they are only stored ephemerally.

    Try it with heroku restart, you will see the new file you created is no longer there

    0 讨论(0)
提交回复
热议问题