Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heroku - how to write into "tmp" directory?

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

like image 430
user984621 Avatar asked Oct 08 '13 16:10

user984621


1 Answers

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...

like image 137
Nick Veys Avatar answered Oct 04 '22 02:10

Nick Veys