2013-10-08 48 views
7

我需要使用在Heroku(雪松)的tmp文件夹写了一些暂时的数据,我想这样做是这样的:Heroku - 如何写入“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 

我想这段代码和它在本地主机上运行正常,但我无法使它在Heroku上工作。

将Heroku(Cedar堆栈)上的某些文件保存到tmp目录的正确方法是什么?

谢谢

编辑: 我跑法延迟乔布斯需要访问的tmp文件。

EDIT2: 我在做什么:

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存储桶中。我需要为这些图像创建缩略图。因此,我正在通过另一个图像浏览一个图像,加载图像,暂时保存图像,然后调整图像大小,之后我会将此缩略图上传到S3存储桶。

但是,这个过程似乎并没有在Heroku上工作,所以,我怎么能这样做(我的应用程序在Heroku上运行)?

+0

既然说有在这个补丁中没有目录,也许你应该创建它在使用之前?或者你想知道heroku的标准tmp在哪里?请记住 – fotanus

+0

,那个heroku对文件系统有严重的限制! https://devcenter.heroku.com/articles/dynos#ephemeral-filesystem – phoet

+0

即使对应用程序的请求创建了一个tmp文件,如果延迟的作业稍后出现,该文件很可能已经消失。 – spickermann

回答

9

/tmp包含在你的git仓库吗?已删除在您的.slugignore? Heroku中的目录可能不存在。在写之前快速的mkdir

尝试折腾:

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

甚至在初始化或东西...

+6

'Rails.root.join('tmp')' – phoet

+0

不错,我没有意识到'root'是'Pathname'。更好。 –