s3.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /*
  3. In order to upload files to S3 using Flash runtime, one should start by placing crossdomain.xml into the bucket.
  4. crossdomain.xml can be as simple as this:
  5. <?xml version="1.0"?>
  6. <!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
  7. <cross-domain-policy>
  8. <allow-access-from domain="*" secure="false" />
  9. </cross-domain-policy>
  10. In our tests SilverLight didn't require anything special and worked with this configuration just fine. It may fail back
  11. to the same crossdomain.xml as last resort.
  12. !!!Important!!! Plupload UI Widget here, is used only for demo purposes and is not required for uploading to S3.
  13. */
  14. // important variables that will be used throughout this example
  15. $bucket = 'BUCKET';
  16. // these can be found on your Account page, under Security Credentials > Access Keys
  17. $accessKeyId = 'ACCESS_KEY_ID';
  18. $secret = 'SECRET_ACCESS_KEY';
  19. // prepare policy
  20. $policy = base64_encode(json_encode(array(
  21. // ISO 8601 - date('c'); generates uncompatible date, so better do it manually
  22. 'expiration' => date('Y-m-d\TH:i:s.000\Z', strtotime('+1 day')),
  23. 'conditions' => array(
  24. array('bucket' => $bucket),
  25. array('acl' => 'public-read'),
  26. array('starts-with', '$key', ''),
  27. // for demo purposes we are accepting only images
  28. array('starts-with', '$Content-Type', 'image/'),
  29. // Plupload internally adds name field, so we need to mention it here
  30. array('starts-with', '$name', ''),
  31. // One more field to take into account: Filename - gets silently sent by FileReference.upload() in Flash
  32. // http://docs.amazonwebservices.com/AmazonS3/latest/dev/HTTPPOSTFlash.html
  33. array('starts-with', '$Filename', ''),
  34. )
  35. )));
  36. // sign policy
  37. $signature = base64_encode(hash_hmac('sha1', $policy, $secret, true));
  38. ?>
  39. <!DOCTYPE html>
  40. <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr">
  41. <head>
  42. <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
  43. <title>Plupload to Amazon S3 Example</title>
  44. <link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/themes/base/jquery-ui.css" type="text/css" />
  45. <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
  46. <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js"></script>
  47. <!-- Load plupload and all it's runtimes and finally the UI widget -->
  48. <link rel="stylesheet" href="../../js/jquery.ui.plupload/css/jquery.ui.plupload.css" type="text/css" />
  49. <!-- production -->
  50. <script type="text/javascript" src="../../js/plupload.full.min.js"></script>
  51. <script type="text/javascript" src="../../js/jquery.ui.plupload/jquery.ui.plupload.js"></script>
  52. <!-- debug
  53. <script type="text/javascript" src="../../js/moxie.js"></script>
  54. <script type="text/javascript" src="../../js/plupload.dev.js"></script>
  55. <script type="text/javascript" src="../../js/jquery.ui.plupload/jquery.ui.plupload.js"></script>
  56. -->
  57. </head>
  58. <body style="font: 13px Verdana; background: #eee; color: #333">
  59. <h1>Plupload to Amazon S3 Example</h1>
  60. <div id="uploader">
  61. <p>Your browser doesn't have Flash, Silverlight or HTML5 support.</p>
  62. </div>
  63. <script type="text/javascript">
  64. // Convert divs to queue widgets when the DOM is ready
  65. $(function() {
  66. $("#uploader").plupload({
  67. runtimes : 'html5,flash,silverlight',
  68. url : 'http://<?php echo $bucket; ?>.s3.amazonaws.com/',
  69. multipart: true,
  70. multipart_params: {
  71. 'key': '${filename}', // use filename as a key
  72. 'Filename': '${filename}', // adding this to keep consistency across the runtimes
  73. 'acl': 'public-read',
  74. 'Content-Type': 'image/jpeg',
  75. 'AWSAccessKeyId' : '<?php echo $accessKeyId; ?>',
  76. 'policy': '<?php echo $policy; ?>',
  77. 'signature': '<?php echo $signature; ?>'
  78. },
  79. // !!!Important!!!
  80. // this is not recommended with S3, since it will force Flash runtime into the mode, with no progress indication
  81. //resize : {width : 800, height : 600, quality : 60}, // Resize images on clientside, if possible
  82. // optional, but better be specified directly
  83. file_data_name: 'file',
  84. filters : {
  85. // Maximum file size
  86. max_file_size : '10mb',
  87. // Specify what files to browse for
  88. mime_types: [
  89. {title : "Image files", extensions : "jpg,jpeg"}
  90. ]
  91. },
  92. // Flash settings
  93. flash_swf_url : '../../js/Moxie.swf',
  94. // Silverlight settings
  95. silverlight_xap_url : '../../js/Moxie.xap'
  96. });
  97. });
  98. </script>
  99. </body>
  100. </html>