Heroku - how to write into "tmp" directory?
Asked Answered
S

2

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

Solomon answered 8/10, 2013 at 16:17 Comment(8)
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?Aalii
keep in mind, that heroku has serious limitations on the file system! devcenter.heroku.com/articles/dynos#ephemeral-filesystemGuild
Even if a request to the app created a tmp file, the file is most likely gone, if the delayed jobs comes along later.Becky
@Becky that should not be true for the new cedar stack. as far as i understand, the filesystem is persistent unless you push some changes.Guild
@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.Becky
@Becky 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?Guild
@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...Becky
@Solomon - I am facing same issue. Were you able to have it working? You help would be much appreciated. Thanks.Amann
T
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...

Turves answered 8/10, 2013 at 16:28 Comment(2)
Rails.root.join('tmp')Guild
Nice, I didn't realize root was a Pathname. Even better.Turves
V
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

Vierno answered 17/2, 2019 at 8:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.