Creating temporary files in Heroku

瘦欲@ 提交于 2020-01-09 06:47:24

问题


I have an app hosted @ Heroku. The app depends on some feeds which are fetched using a socket listener. The socket listener gets one line of XML per second. Once I detect the end of file signal from the listener, I upload the file to Amazon S3 servers. But, until the end of file signal is received, is it possible to save the file content as a temporary file in Heroku?


回答1:


You may be able to use the #{RAILS_ROOT}/tmp/ directory or Rails.root.join('tmp').to_s:

Aspen & Bamboo
[...]
There are two directories that are writeable: ./tmp and ./log (under your application root).
[...]

Cedar
Cedar offers an ephemeral writeable filesystem. You can write out to disk anywhere you would like. Your changes will be lost on dyno restart and spin-up.

RAILS_ROOT is for older Rails versions, Rails.root is for newer versions.

You can't depend on anything surviving across requests of course, there's no guarantee that you'll even be working with the same dyno.

As long as you stay within the same process or request, Rails.root.join('tmp') should be usable. If you need the temporary data to survive across requests or processes then you're better off using something else (such as MongoDB or PostgreSQL) as a collecting ground for your data on its way to S3.


Thanks to Benjamin Wheeler for the heads up about the RAILS_ROOT to Rails.root change.




回答2:


The documentation on Heroku's read-only file system explains that you can use #{RAILS_ROOT}/tmp but doesn't give any examples for generating a temporary filename. This will guarantee uniqueness of the filename:

prefix = 'mydata'
suffix = '.xml'
Tempfile.new [prefix, suffix], "#{Rails.root}/tmp"

Note that "there is no guarantee that this file will be there on subsequent requests (although it might be), so this should not be used for any kind of permanent storage."




回答3:


With the newer Heroku-16 stack, you are able to write to both the root and to /tmp

Try writing to root with

f = File.new("filename.txt", 'w')
f << "hi there"
f.close

Dir.entries(Dir.pwd) # see your newly created file

Or to /tmp with

f = File.new("tmp/filename.txt", 'w')
f << "hi there"
f.close

Dir.entries(Dir.pwd.to_s + ("/tmp"))

You will see your new file among those listed in both cases

Also try running heroku restart to see your newly created files disappear! This is expected, as heroku storage is ephemeral (will be deleted when the app restarts) - so don't rely on it for anything more than (very) temporary storage



来源:https://stackoverflow.com/questions/6661395/creating-temporary-files-in-heroku

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!