s3.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. // hash_hmac — Generate a keyed hash value using the HMAC method
  20. // (PHP 5 >= 5.1.2, PECL hash >= 1.1)
  21. if (!function_exists('hash_hmac')) :
  22. // based on: http://www.php.net/manual/en/function.sha1.php#39492
  23. function hash_hmac($algo, $data, $key, $raw_output = false)
  24. {
  25. $blocksize = 64;
  26. if (strlen($key) > $blocksize)
  27. $key = pack('H*', $algo($key));
  28. $key = str_pad($key, $blocksize, chr(0x00));
  29. $ipad = str_repeat(chr(0x36), $blocksize);
  30. $opad = str_repeat(chr(0x5c), $blocksize);
  31. $hmac = pack('H*', $algo(($key^$opad) . pack('H*', $algo(($key^$ipad) . $data))));
  32. return $raw_output ? $hmac : bin2hex($hmac);
  33. }
  34. endif;
  35. // prepare policy
  36. $policy = base64_encode(json_encode(array(
  37. // ISO 8601 - date('c'); generates uncompatible date, so better do it manually
  38. 'expiration' => date('Y-m-d\TH:i:s.000\Z', strtotime('+1 day')),
  39. 'conditions' => array(
  40. array('bucket' => $bucket),
  41. array('acl' => 'public-read'),
  42. array('starts-with', '$key', ''),
  43. // for demo purposes we are accepting only images
  44. array('starts-with', '$Content-Type', 'image/'),
  45. // "Some versions of the Adobe Flash Player do not properly handle HTTP responses that have an empty body.
  46. // To configure POST to return a response that does not have an empty body, set success_action_status to 201.
  47. // When set, Amazon S3 returns an XML document with a 201 status code."
  48. // http://docs.amazonwebservices.com/AmazonS3/latest/dev/HTTPPOSTFlash.html
  49. array('success_action_status' => '201'),
  50. // Plupload internally adds name field, so we need to mention it here
  51. array('starts-with', '$name', ''),
  52. // One more field to take into account: Filename - gets silently sent by FileReference.upload() in Flash
  53. // http://docs.amazonwebservices.com/AmazonS3/latest/dev/HTTPPOSTFlash.html
  54. array('starts-with', '$Filename', ''),
  55. )
  56. )));
  57. // sign policy
  58. $signature = base64_encode(hash_hmac('sha1', $policy, $secret, true));
  59. ?>
  60. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  61. <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr">
  62. <head>
  63. <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
  64. <title>Plupload to Amazon S3 Example</title>
  65. <style type="text/css">
  66. body {
  67. font-family:Verdana, Geneva, sans-serif;
  68. font-size:13px;
  69. color:#333;
  70. background:url(../bg.jpg);
  71. }
  72. </style>
  73. <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/themes/base/jquery-ui.css" type="text/css" />
  74. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
  75. <script src=" https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js"></script>
  76. <!-- Load plupload and all it's runtimes and finally the UI widget -->
  77. <link rel="stylesheet" href="../../js/jquery.ui.plupload/css/jquery.ui.plupload.css" type="text/css" />
  78. <script type="text/javascript" src="../../js/plupload.js"></script>
  79. <script type="text/javascript" src="../../js/plupload.gears.js"></script>
  80. <script type="text/javascript" src="../../js/plupload.silverlight.js"></script>
  81. <script type="text/javascript" src="../../js/plupload.flash.js"></script>
  82. <script type="text/javascript" src="../../js/plupload.browserplus.js"></script>
  83. <script type="text/javascript" src="../../js/plupload.html4.js"></script>
  84. <script type="text/javascript" src="../../js/plupload.html5.js"></script>
  85. <script type="text/javascript" src="../../js/jquery.ui.plupload/jquery.ui.plupload.js"></script>
  86. <!--<script type="text/javascript" src="http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js"></script>-->
  87. </head>
  88. <body>
  89. <h1>Plupload to Amazon S3 Example</h1>
  90. <div id="uploader">
  91. <p>You browser doesn't have Flash, Silverlight, Gears, BrowserPlus or HTML5 support.</p>
  92. </div>
  93. <script type="text/javascript">
  94. // Convert divs to queue widgets when the DOM is ready
  95. $(function() {
  96. $("#uploader").plupload({
  97. runtimes : 'flash,silverlight',
  98. url : 'http://<?php echo $bucket; ?>.s3.amazonaws.com/',
  99. max_file_size : '10mb',
  100. multipart: true,
  101. multipart_params: {
  102. 'key': '${filename}', // use filename as a key
  103. 'Filename': '${filename}', // adding this to keep consistency across the runtimes
  104. 'acl': 'public-read',
  105. 'Content-Type': 'image/jpeg',
  106. 'success_action_status': '201',
  107. 'AWSAccessKeyId' : '<?php echo $accessKeyId; ?>',
  108. 'policy': '<?php echo $policy; ?>',
  109. 'signature': '<?php echo $signature; ?>'
  110. },
  111. // !!!Important!!!
  112. // this is not recommended with S3, since it will force Flash runtime into the mode, with no progress indication
  113. //resize : {width : 800, height : 600, quality : 60}, // Resize images on clientside, if possible
  114. // optional, but better be specified directly
  115. file_data_name: 'file',
  116. // re-use widget (not related to S3, but to Plupload UI Widget)
  117. multiple_queues: true,
  118. // Specify what files to browse for
  119. filters : [
  120. {title : "JPEG files", extensions : "jpg"}
  121. ],
  122. // Flash settings
  123. flash_swf_url : '../../js/plupload.flash.swf',
  124. // Silverlight settings
  125. silverlight_xap_url : '../../js/plupload.silverlight.xap'
  126. });
  127. });
  128. </script>
  129. </body>
  130. </html>