将文件存储在heroku Rails的目录tmp中 [英] Store file in directory tmp on heroku Rails

查看:133
本文介绍了将文件存储在heroku Rails的目录tmp中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b

  file_path = Rails.root.join(' tmp',#{file_name}。+ file_extension); 
exported_file = kit.to_pdf
#将文件保存到磁盘
File.open(file_path,'wb')do | file |
档案<< exported_file
end

它在本地运行良好,但在Heroku中延迟作业
没有这样的文件或目录 - /app/tmp/test.pdf

那么我该如何解决这个问题。我不想在S3中存储文件。
谢谢

解决方案

Heroku使用所谓的临时文件系统。这意味着你的本地文件系统只能被一个dyno访问,一旦dyno停止,重新启动或移动,本地文件系统上的所有文件都将被销毁。



延迟作业进程将文件传输到外部进程的唯一方法是将文件存储在更持久的位置。这可能是S3,一个数据库,Rackspace Files等,但是对于Heroku,它不能是本地文件系统。

然而,如果你只是想将文件存储在一个临时的scratch位置,使用本地文件系统很好。看起来你可能会遇到问题,因为 / app / tmp 目录可能不存在。



尝试添加给您的工作人员:

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

或者,将 tmp 目录添加到您的git存储库。

  mkdir tmp 
touch tmp / .keep
git add tmp / .keep
git commit -m将tmp目录添加到应用程序库。


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

解决方案

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."

这篇关于将文件存储在heroku Rails的目录tmp中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆