views:

37

answers:

2

I'm trying to make an image of a graph using ruby-graphviz by

@graph.output(:output => "png", :file => "public/images/graph.png")

Since I'm using heroku, how can I save the image locally and send it to an s3 bucket, or just send it straight to the s3 bucket? I'd appreciate any help.

A: 

There are two directories that are writeable: ./tmp and ./log (under your application root). If you wish to drop a file temporarily for the duration of the request, you can write to a filename like #{RAILS_ROOT}/tmp/myfile_#{Process.pid}. There is no guarantee that this file will be there on subsequent requests (although it might be), so this should not be used for any kind of permanent storage.

http://docs.heroku.com/constraints

James
A: 

So you can write to the tmp/ directory under your application root, as long as you don't expect it to be there on the next request. So you have to do everything in one request cycle (or one delayed job).

Here's what you would need to do. Run whatever script to generate your file and write it out to tmp/. Then use something like Paperclip with the S3 backend to save the file to S3. Make sure you use the S3 storage backend (Paperclip uses the FS by default); instead of flushing writes out to the file system, it uses the AWS::S3 gem to upload them to S3. You can read more about it in the rdoc here: http://github.com/thoughtbot/paperclip/blob/master/lib/paperclip/storage/s3.rb

Or you can always upload it to S3 yourself using whatever gem or library you like.

I use this method in a couple production web apps. You can't count on tmp/ being there between requests, but within one it's reliably there.

tfe