将文件存储在 heroku Rails 上的 tmp 目录中 [英] Store file in directory tmp on heroku Rails
问题描述
在我的延迟作业中,我尝试在 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
它在本地运行良好,但在 Heroku 上延迟作业有错误没有这样的文件或目录 -/app/tmp/test.pdf"
It works well in local but on Heroku there is a error in Delayed Job "No such file or directory - /app/tmp/test.pdf"
那么我该如何解决这个问题.我不想在 S3 中存储文件.谢谢
So how I can solve this problem. I do not want to store file in S3. Thank you
推荐答案
Heroku 使用所谓的 临时文件系统.这意味着您的本地文件系统只能被单个 dyno 访问,并且一旦 dyno 停止、重新启动或移动,本地文件系统上的所有文件都会被销毁.
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.
延迟作业进程将文件传输到外部进程的唯一方法是将文件存储在更永久的位置.这可能是 S3、数据库、Rackspace 文件等,但对于 Heroku,它不能是本地文件系统.
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.
但是,如果您只是想将文件存储在临时暂存位置,则可以使用本地文件系统.您可能遇到了问题,因为 /app/tmp
目录可能不存在.
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'))
或者,将 tmp
目录添加到您的 git 存储库.
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屋!