Heroku - 如何写入“tmp”目录? [英] Heroku - how to write into "tmp" directory?

查看:145
本文介绍了Heroku - 如何写入“tmp”目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用Heroku(Cedar)上的 tmp 文件夹来编写一些临时数据,我试图这样做:

  open(#{Rails.root} / tmp /#{result ['filename']},'wb')do | file | 
file.write open(image_url).read
end

错误

  Errno :: ENOENT:没有这样的文件或目录 -  /app/tmp/image-2.png 

我正在尝试这段代码,它在localhost上正常运行,但我无法在Heroku上运行。



保存一些文件到Heroku(Cedar stack)的 tmp 目录的正确方法是什么?



谢谢

编辑:
我正在运行需要延迟作业的方法访问tmp文件。



EDIT2:
我在做什么:

  files.each_with_index do | f,index | 
除非f.nil?
result = JSON.parse(buffer)
filename =#{Time.now.to_i.to_s} _#{result ['filename']}#缩略图名称
thumb_filename = #{Rails.root} / tmp /#{filename}

image_url = f.file_url +/ convert?rotate = exif

open 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)

#将新缩略图的路径保存到数据库
f.update_attributes(:file_url_thumb =>https://s3-us-west-1.amazonaws.com/bucket /#{filename})
end
end

有关图像的信息。这些图像存储在Amazon S3存储桶中。我需要为这些图像创建缩略图。因此,我正在通过另一个图像浏览一个图像,加载图像,暂时保存图像,然后调整图像大小,之后我会将此缩略图上传到S3存储区。



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

解决方案

是否包含在你的git仓库中的 / tmp ?已删除在 .slugignore 中?



p

尝试在写入之前在快速mkdir中进行折腾:

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

或甚至在初始化器或其他东西中......


I need to use the tmp folder on Heroku (Cedar) for writing some temporarily data, I am trying to do that this way:

open("#{Rails.root}/tmp/#{result['filename']}", 'wb') do |file|
  file.write open(image_url).read 
end

But this produce error

Errno::ENOENT: No such file or directory - /app/tmp/image-2.png

I am trying this code and it's running properly on localhost, but I cannot make it work on Heroku.

What is the proper way to save some files to the tmp directory on Heroku (Cedar stack)?

Thank you

EDIT: I am running method with Delayed Jobs that needs to has access to the tmp file.

EDIT2: What I am doing:

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

I have in database information about images. These images are stored in Amazon S3 bucket. I need to create thumbnails to these images. So I am going through one image by another one, load the image, temporarily save it, then resize it and afterwards I will upload this thumbnail to S3 bucket.

But this procedure doesn't seems to be working on Heroku, so, how could I do that (my app is running on Heroku)?

解决方案

Is /tmp included in your git repo? Removed in your .slugignore? The directory may just not exist out on Heroku.

Try tossing in a quick mkdir before the write:

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

Or even in an initializer or something...

这篇关于Heroku - 如何写入“tmp”目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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