Heroku – 如何写入“tmp”目录?
我需要使用Heroku(Cedar)上的tmp
文件夹来写一些临时数据,我试着这样做:
open("#{Rails.root}/tmp/#{result['filename']}", 'wb') do |file| file.write open(image_url).read end
但这会产生错误
Errno::ENOENT: No such file or directory - /app/tmp/image-2.png
我正在尝试这个代码,它在localhost上正常运行,但我不能让它在Heroku上运行。
将一些文件保存到Heroku(Cedar堆栈)上的tmp
目录的正确方法是什么?
谢谢
编辑:我正在运行需要访问tmp文件的延迟作业的方法。
编辑2:我在做什么:
files.each_with_index do |f, index| unless f.nil? result = JSON.parse(buffer) filename = "#{Time.now.to_i.to_s}_#{result['filename']}" # thumbnail name thumb_filename = "#{Rails.root}/tmp/#{filename}" image_url = f.file_url+"/convert?rotate=exif" open("#{Rails.root}/tmp/#{result['filename']}", 'wb') do |file| file.write open(image_url).read end img = Magick::Image.read(image_url).first target = Magick::Image.new(150, 150) do self.background_color = 'white' end img.resize_to_fit!(150, 150) target.composite(img, Magick::CenterGravity, Magick::CopyCompositeOp).write(thumb_filename) key = File.basename(filename) s3.buckets[bucket_name].objects[key].write(:file => thumb_filename) # save path to the new thumbnail to database f.update_attributes(:file_url_thumb => "https://s3-us-west-1.amazonaws.com/bucket/#{filename}") end end
我有关于图像的数据库信息。 这些图像存储在Amazon S3存储桶中。 我需要为这些图像创建缩略图。 因此,我将通过另一个图像,加载图像,暂时保存,然后resize,然后我将此缩略图上传到S3存储桶。
但是这个程序似乎不适用于Heroku,所以,我怎么能这样做(我的应用程序在Heroku上运行)?
/tmp
是否包含在您的git仓库中? 删除你的.slugignore
? 该目录可能在Heroku上不存在。
在写之前尝试在快速mkdir中折腾:
Dir.mkdir(File.join(Rails.root, 'tmp'))
甚至在初始化器或其他东西……