ruby-on-rails – Heroku – 如何写入“tmp”目录?
发布时间:2020-12-16 19:20:00 所属栏目:百科 来源:网络整理
导读:我需要使用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/ima
我需要使用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目录的正确方法是什么? 谢谢 编辑: 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上运行)? 解决方法
/ tmp是否包含在您的git仓库中?删除你的.slugignore?该目录可能在Heroku上不存在.
在写之前尝试在快速mkdir中折腾: Dir.mkdir(File.join(Rails.root,'tmp')) 甚至在初始化器或其他东西…… (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |