Client uploads to Amazon S3

As part of the spangly new and exciting project I’m working on, I’ve got a dumb JRuby client app that runs on the user’s desktop, which I need to have upload data to my S3 buckets. I pondered and read for a bit. S3 is new to me, so I was wandering off down the path of “touch the key with a public-writeable ACL, wait for a completion notification callback, then close the ACL.” This, obviously, is madness.

Luckily, the fine folks at Amazon have already thought of this, and provided a POST mechanism designed for browsers. It’s got a slightly strange gotcha, though: the uploaded file must be the last element in the POST body. This and Ruby’s default Net::HTTP API sent me looking for alternatives to building the post by hand.

The solution is quite neat. I’ve got a teeny Sinatra app sitting on the server whose only purpose is to serve prevalidated upload forms to authenticated clients. Auth is provided by Rack. The client just uses 6 lines of Mechanize code to fill in the form details and submit it to S3. It’s rather a library-heavy solution, but as a concept it doesn’t get much simpler.

And simpler is better.

Leave a Reply

Entries (RSS)