18

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

user984621
  • 46,344
  • 73
  • 224
  • 412
  • Since it says that there is no directory in this patch, maybe you should create it before using? Or do you want to know where is the standard tmp on heroku? – fotanus Oct 08 '13 at 16:21
  • keep in mind, that heroku has serious limitations on the file system! https://devcenter.heroku.com/articles/dynos#ephemeral-filesystem – phoet Oct 08 '13 at 16:29
  • Even if a request to the app created a tmp file, the file is most likely gone, if the delayed jobs comes along later. – spickermann Oct 08 '13 at 16:29
  • @spickermann that should not be true for the new cedar stack. as far as i understand, the filesystem is persistent unless you push some changes. – phoet Oct 08 '13 at 17:46
  • @phoet: If a server creates a tmp file (perhaps because a user uploads a file), the server must delete the file after the request is complete. Otherwise the tmp folder will get bigger and bigger. This behavior has nothing to do with Heroku. – spickermann Oct 08 '13 at 19:37
  • @spickermann that is true for fileuploads, but in this case, we are not talking about file uploads, or did i misread anything? another thing that comes to my mind is: why are you not using paperclip? – phoet Oct 09 '13 at 07:07
  • @phoet, you are right. I wrote my comment before the second edit. Here it is just a file in the tmp folder not a tempfile... – spickermann Oct 09 '13 at 07:50
  • @user984621 - I am facing same issue. Were you able to have it working? You help would be much appreciated. Thanks. – varunvlalan Nov 07 '14 at 08:04

2 Answers2

17

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

Nick Veys
  • 23,458
  • 4
  • 47
  • 64
1

Here's an elegant way

f = File.new("tmp/filename.txt", 'w')
f << "hi there"
f.close

Dir.entries(Dir.pwd.to_s + ("/tmp")) # See your newly created file in /tmp

Don't forget that whenever your app restarts (for any reason, including those outside your control), your files will be deleted, as they are only stored ephemerally.

Try it with heroku restart, you will see the new file you created is no longer there

stevec
  • 41,291
  • 27
  • 223
  • 311