This is just a sample app using Rails 3 which allows you to upload directly to Amazon S3 and bypass the Rails server entirely. This is useful for cloud hosting like Heroku which has a 30 second request timeout and resets the connection on any uploads which go over that limit.
- Uploading: PLUpload
- Uploading Technology: jQuery API using Flash, Google Gears, Silverlight, BrowserPlus, HTML5 to do the heavy lifting
Note: Currently Amazon S3 DOES NOT support HTML5 uploading. More info here
For debugging purposes involving Flash uploading, I would highly recommend using the WireShark tool which allows you to sniff the network traffic for XML responses sent back from Amazon S3.
Link to download WireShark: http://www.wireshark.org/download.html
How to install on MacOSX: http://www.youtube.com/watch?v=IxeHm0BKdwc
Amazon S3 will always send back an XML response to the Flash uploader and currently the uploader does nothing with it except give a vague error response which isn't helpful.
So what you have to do is first close out other items that may be using the internet at the moment (to minimize noise) and then run WireShark and capture the network traffic while you try and upload a file. You'll start to see a whole bunch of stuff show up in WireShark so go ahead and filter it out by typing "xml" into the Filter box at the top of the window. This will show only the XML responses back from S3 like this: http://i.imgur.com/VZ5ZC.png
The one that is highlighted in that image is a 403 forbidden response which could mean that you are missing something in your Amazon S3 policy or that you have extra keys that it doesn't know about. Some uploaders actually send extra data with the request (like the name item shown) that Amazon doesn't know about so you have to tell it to accept the params with any value, which is what the blank starts-with
in the policy item does:
['starts-with', '$name', ''],
Hopefully WireShark helps you solve many headaches in the future. Happy uploading!
-
Clone this repo and run
bundle install
-
Add your S3 bucket and keys to
config/amazon_s3.yml
-
For Flash: Make sure a public readable
crossdomain.xml
exists in the root directory of the S3 bucket you specified inconfig/amazon_s3.yml
, with the following content (once you get it working, you should limit access to your domain to be more secure). An example can be found here -
For Silverlight: Make sure a public readable
clientaccesspolicy.xml
exists in the root directory of the S3 bucket you specified inconfig/amazon_s3.yml
, with the following content (once you get it working, you should limit access to your domain to be more secure). An example can be found here -
Also make sure a folder named test exists in that bucket (if not, one will be created). Files will be uploaded to that folder.
-
Run the server locally or push it up to Heroku and start uploading files.
-
If you want to explore the code, the bulk of it is in the
helpers/uploads_helper.rb
file and then a convenient helper for using/customizing the uploader can be found in theviews/uploads/new.html.haml
file
Note PLUpload uses CSS to absolutely position the Flash/Silverlight objects over top of your custom "Upload" button, so if you move that button at all during the upload process the Flash/Silverlight object will still be there.
If you would like to generate a secure, time-sensitive URL for accessing an object after you are finished uploading the file, here is a gist file which will help you to do so: https://gist.github.com/1032395
One thing to remember is that you should be setting the ACL of the file to private when you upload it so that no one else can access it without that signed URL.
I am always open to others collaborating on this example and I welcome any pull requests. If you find any issues, please report them in the Issues area above. Thanks!!
Nico Ritsche: https://github.com/ncri
http://www.railstoolkit.com/posts/rails-amazon-s3-uploader-with-progress-bar-2010-update