views:

488

answers:

2

I have a question about hosting large dynamically-generated assets and Heroku.

My app will offer bulk download of a subset of its underlying data, which will consist of a large file (>100 MB) generated once every 24 hours. If I were running on a server, I'd just write the file into the public directory.

But as I understand it, this is not possible with Heroku. The /tmp directory can be written to, but the guaranteed lifetime of files there seems to be defined in terms of one request-response cycle, not a background job.

I'd like to use S3 to host the download file. The S3 gem does support streaming uploads, but only for files that already exist on the local filesystem. It looks like the content size needs to be known up-front, which won't be possible in my case.

So this looks like a catch-22. I'm trying to avoid creating a gigantic string in memory when uploading to S3, but S3 only supports streaming uploads for files that already exist on the local filesystem.

Given a Rails app in which I can't write to the local filesystem, how do I serve a large file that's generated daily without creating a large string in memory?

+2  A: 

Rich,

Have you tried writing the file to ./tmp then streaming the file to S3?

-Blake Mizerany (Heroku)

Blake Mizerany
@Blake, I haven't tried because the Heroku documentation on the /tmp directory (linked above) says (from what I can tell) that the contents of the directory will survive for the duration of one request. What I'd be doing would be in the context of a background job, which seems like asking for trouble. If I'm wrong, then great - that would solve the problem.
Rich Apodaca
+6  A: 

${RAILS_ROOT}/tmp (not /tmp, it's in your app's directory) lasts for the duration of your process. If you're running a background DJ, the files in TMP will last for the duration of that process.

Actually, the files will last longer, the reason we say you can't guarantee availability is that tmp isn't shared across servers, and each job/process can run on a different server based on the cloud load. You also need to make sure you delete your files when you're done with them as part of the job.

-Another Heroku employee

teich
@teich, that makes sense- thanks!
Rich Apodaca