s3.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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>
  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. <link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/themes/base/jquery-ui.css" type="text/css" />
  66. <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
  67. <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js"></script>
  68. <!-- Load plupload and all it's runtimes and finally the UI widget -->
  69. <link rel="stylesheet" href="../../js/jquery.ui.plupload/css/jquery.ui.plupload.css" type="text/css" />
  70. <!-- production -->
  71. <script type="text/javascript" src="../../js/plupload.full.min.js"></script>
  72. <script type="text/javascript" src="../../js/jquery.ui.plupload/jquery.ui.plupload.js"></script>
  73. <!-- debug
  74. <script type="text/javascript" src="../../js/moxie.js"></script>
  75. <script type="text/javascript" src="../../js/plupload.dev.js"></script>
  76. <script type="text/javascript" src="../../js/jquery.ui.plupload/jquery.ui.plupload.js"></script>
  77. -->
  78. </head>
  79. <body style="font: 13px Verdana; background: #eee; color: #333">
  80. <h1>Plupload to Amazon S3 Example</h1>
  81. <div id="uploader">
  82. <p>Your browser doesn't have Flash, Silverlight or HTML5 support.</p>
  83. </div>
  84. <script type="text/javascript">
  85. // Convert divs to queue widgets when the DOM is ready
  86. $(function() {
  87. $("#uploader").plupload({
  88. runtimes : 'html5,flash,silverlight',
  89. url : 'http://<?php echo $bucket; ?>.s3.amazonaws.com/',
  90. multipart: true,
  91. multipart_params: {
  92. 'key': '${filename}', // use filename as a key
  93. 'Filename': '${filename}', // adding this to keep consistency across the runtimes
  94. 'acl': 'public-read',
  95. 'Content-Type': 'image/jpeg',
  96. 'success_action_status': '201',
  97. 'AWSAccessKeyId' : '<?php echo $accessKeyId; ?>',
  98. 'policy': '<?php echo $policy; ?>',
  99. 'signature': '<?php echo $signature; ?>'
  100. },
  101. // !!!Important!!!
  102. // this is not recommended with S3, since it will force Flash runtime into the mode, with no progress indication
  103. //resize : {width : 800, height : 600, quality : 60}, // Resize images on clientside, if possible
  104. // optional, but better be specified directly
  105. file_data_name: 'file',
  106. filters : {
  107. // Maximum file size
  108. max_file_size : '10mb',
  109. // Specify what files to browse for
  110. mime_types: [
  111. {title : "Image files", extensions : "jpg,jpeg"}
  112. ]
  113. },
  114. // Flash settings
  115. flash_swf_url : '../../js/Moxie.swf',
  116. // Silverlight settings
  117. silverlight_xap_url : '../../js/Moxie.xap'
  118. });
  119. });
  120. </script>
  121. </body>
  122. </html>