s3.php 4.6 KB

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