Ruby-on-rails 在 Heroku 中创建临时文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6661395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating temporary files in Heroku
提问by Satya Kalluri
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?
我有一个应用程序托管@ Heroku。该应用程序依赖于使用套接字侦听器获取的一些提要。套接字侦听器每秒获取一行 XML。检测到来自侦听器的文件结束信号后,我将文件上传到 Amazon S3 服务器。但是,在收到文件结束信号之前,是否可以将文件内容保存为 Heroku 中的临时文件?
回答by mu is too short
You may be able to use the #{RAILS_ROOT}/tmp/directoryor Rails.root.join('tmp').to_s:
您可以使用该#{RAILS_ROOT}/tmp/目录或Rails.root.join('tmp').to_s:
Aspen & Bamboo
[...]
There are two directories that are writeable:./tmpand./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.
Aspen & Bamboo
[...]
有两个可写目录:./tmp和./log(在您的应用程序根目录下)。
[...]Cedar
Cedar 提供了一个短暂的可写文件系统。你可以在任何你想要的地方写出到磁盘。您的更改将在 dyno 重新启动和启动时丢失。
RAILS_ROOTis for older Rails versions, Rails.rootis for newer versions.
RAILS_ROOT适用于较旧的 Rails 版本,Rails.root适用于较新的版本。
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.
当然,您不能依赖任何跨请求幸存下来的东西,不能保证您甚至会使用相同的 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.
只要你停留在同一个进程或请求中,Rails.root.join('tmp')应该是可用的。如果您需要临时数据在请求或进程中存活,那么您最好使用其他东西(例如 MongoDB 或 PostgreSQL)作为数据到 S3 的收集地。
Thanks to Benjamin Wheelerfor the heads up about the RAILS_ROOTto Rails.rootchange.
感谢本杰明·惠勒的抬起头对RAILS_ROOT来Rails.root改变。
回答by Turadg
The documentation on Heroku's read-only file systemexplains that you can use #{RAILS_ROOT}/tmpbut doesn't give any examples for generating a temporary filename. This will guarantee uniqueness of the filename:
Heroku 只读文件系统的文档解释了您可以使用#{RAILS_ROOT}/tmp但没有提供任何示例来生成临时文件名。这将保证文件名的唯一性:
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."
请注意,“不能保证此文件会在后续请求中存在(尽管可能存在),因此不应将其用于任何类型的永久存储。”
回答by stevec
With the newer Heroku-16stack, you are able to write to both the root andto /tmp
随着新的Heroku-16堆栈,你可以写两个根和到/tmp
Try writing to root with
尝试写入 root
f = File.new("filename.txt", 'w')
f << "hi there"
f.close
Dir.entries(Dir.pwd) # see your newly created file
Or to /tmpwith
或/tmp与
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 restartto 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
也试着运行heroku restart看看你新创建的文件消失了!这是意料之中的,因为 heroku 存储是短暂的(将在应用程序重新启动时被删除) - 所以除了(非常)临时存储之外不要依赖它

