/README.md

http://github.com/slaskis/s3upload · Markdown · 97 lines · 73 code · 24 blank · 0 comment · 0 complexity · 1e292db0963e8b31c262aca6bb358e83 MD5 · raw file

  1. S3Upload
  2. ========
  3. A jQuery plugin for direct upload to an Amazon S3 bucket.
  4. It works by replacing a file input with a html element overlaid with a transparent SWF. The same way [Flickr](http://www.flickr.com/photos/upload/) does it.
  5. By signing the request server side we also avoid the security issue of showing the Amazon AWS Access Id Key and Secure Key in plain text. A library for signing the request in Ruby is included, but the logic should be very easy to replicate in other languages like PHP or Python.
  6. The Javascript API also allows these callback functions:
  7. * onselect(info) - Called when a user has selected a file.
  8. * oncancel(info) - Called if the user decides to abort the file browsing.
  9. * onstart(info) - Called after the request has been signed and the file upload to S3 is starting.
  10. * onprogress(progress,info) - Called while uploading, "progress" being a float between 0 and 1 of the current upload progress.
  11. * oncomplete(info) - Called when the upload has finished successfully.
  12. * onerror(msg,info) - Called if there's been a problem with a message saying what failed.
  13. * onenabled() - Called when the SWF has been enabled. Usually when swf.enable() has been called. Called first thing when the SWF is finished initializing.
  14. * ondisabled() - Called when the SWF has been disabled. Usually when swf.disable() has been called.
  15. _info_ is an object containing "name", "size" and "type" of the selected file.
  16. And these mouse callbacks:
  17. * onmouseover(x,y)
  18. * onmouseout(x,y)
  19. * onmousedown(x,y)
  20. * onmouseup(x,y)
  21. * onmousemove(x,y)
  22. * onclick(x,y)
  23. * onrollover(x,y)
  24. * onrollout(x,y)
  25. * ondoubleclick(x,y)
  26. The mouse events are also triggered as regular jQuery events (i.e. `$('#input_replaced').rollover(function(){alert('over!')});` should work just fine as well).
  27. Every callback is scoped to the DOM element which has replaced the previous input (i.e. "this" in the callbacks points to the html element). Also by returning `true` in a callback function the default callback will be used.
  28. Which file types that can be selected may be defined with the _file\_types_ option, see the "Usage Example" below for more info. If none is defined all files are acceptable.
  29. Requirements
  30. -------------
  31. * jQuery 1.3+
  32. * SWFObject 2.1+
  33. Both available from Google AJAX APIs (recommended as it likely speeds things up).
  34. Example Usage
  35. -------------
  36. The HTML/JS part:
  37. <script type="text/javascript" charset="utf-8" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
  38. <script type="text/javascript" charset="utf-8" src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script>
  39. <script type="text/javascript" charset="utf-8" src="jquery.s3upload.js"></script>
  40. <script type="text/javascript" charset="utf-8">
  41. $(function(){
  42. var max_file_size = 2 * 1024 * 1024; // = 2Mb
  43. $("form").s3upload({
  44. prefix: "s3upload/",
  45. required: true,
  46. onselect: function(info) {
  47. if( parseInt( info.size ) < max_file_size )
  48. return true; // Default is to show the filename in the element.
  49. else
  50. $(this).html("Too big file! Must be smaller than " + max_file_size + " (was "+info.size+")");
  51. },
  52. file_types: [
  53. [ "Images" , "*.png;*.jpg;*.bmp"],
  54. [ "Documents" , "*.pdf;*.doc;*.txt"]
  55. ]
  56. });
  57. });
  58. </script>
  59. <form action="/media/new" method="post" accept-charset="utf-8" enctype="multipart/form-data">
  60. <label for="media_title">Title</label>
  61. <input type="text" name="media[title]" value="" id="media_title" />
  62. <label for="media_video">Video</label>
  63. <input type="file" name="media[video]" value="" id="media_video" />
  64. <label for="media_thumbnail">Thumbnail</label>
  65. <input type="file" name="media[thumbnail]" value="" id="media_thumbnail" />
  66. <input type="submit" value="Upload" />
  67. </form>
  68. The Sinatra part (assumes the _s3upload_ gem is installed):
  69. require "s3upload"
  70. get "/s3upload" do
  71. up = S3::Upload.new( options.s3_upload_access_key_id , options.s3_upload_secret_key , options.s3_upload_bucket )
  72. up.to_xml( params[:key] , params[:contentType] )
  73. end