modaws.class.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /*
  3. * This file is part of MODX Revolution.
  4. *
  5. * Copyright (c) MODX, LLC. All Rights Reserved.
  6. *
  7. * For complete copyright and license information, see the COPYRIGHT and LICENSE
  8. * files found in the top-level directory of this distribution.
  9. */
  10. /**
  11. * Handles and encapsulates all S3 operations
  12. *
  13. * @package aws
  14. */
  15. class modAws {
  16. /**
  17. * The AmazonS3 class instance
  18. * @var AmazonS3 $s3
  19. */
  20. public $s3 = null;
  21. /**
  22. * The current selected bucket
  23. * @var string $bucket
  24. */
  25. public $bucket = '';
  26. /**
  27. * @param modX $modx A reference to the modX instance
  28. * @param array $config An array of configuration properties
  29. */
  30. function __construct(modX &$modx,array $config = array()) {
  31. $this->modx =& $modx;
  32. $this->config = array_merge(array(
  33. ),$config);
  34. if (!defined('AWS_KEY')) {
  35. define('AWS_KEY',$modx->getOption('aws.key',$config,''));
  36. define('AWS_SECRET_KEY',$modx->getOption('aws.secret_key',$config,''));
  37. /* (Not needed at this time)
  38. define('AWS_ACCOUNT_ID',$modx->getOption('aws.account_id',$config,''));
  39. define('AWS_CANONICAL_ID',$modx->getOption('aws.canonical_id',$config,''));
  40. define('AWS_CANONICAL_NAME',$modx->getOption('aws.canonical_name',$config,''));
  41. define('AWS_MFA_SERIAL',$modx->getOption('aws.mfa_serial',$config,''));
  42. define('AWS_CLOUDFRONT_KEYPAIR_ID',$modx->getOption('aws.cloudfront_keypair_id',$config,''));
  43. define('AWS_CLOUDFRONT_PRIVATE_KEY_PEM',$modx->getOption('aws.cloudfront_private_key_pem',$config,''));
  44. define('AWS_ENABLE_EXTENSIONS', 'false');*/
  45. }
  46. include dirname(__FILE__).DIRECTORY_SEPARATOR.'sdk.class.php';
  47. $this->getS3();
  48. $this->setBucket($modx->getOption('aws.default_bucket',$config,''));
  49. }
  50. /**
  51. * Get the AmazonS3 client class instance
  52. *
  53. * @return AmazonS3|null
  54. */
  55. public function getS3() {
  56. if (empty($this->s3)) {
  57. try {
  58. $this->s3 = new AmazonS3();
  59. } catch (Exception $e) {
  60. $this->modx->log(modX::LOG_LEVEL_ERROR,'[modAws] Could not load AmazonS3 class: '.$e->getMessage());
  61. }
  62. }
  63. return $this->s3;
  64. }
  65. /**
  66. * Set the bucket for the connection to S3
  67. * @param string $bucket
  68. * @return void
  69. */
  70. public function setBucket($bucket) {
  71. $this->bucket = $bucket;
  72. }
  73. /**
  74. * Check with S3 to confirm the currently selected bucket exists
  75. *
  76. * @return bool
  77. */
  78. public function bucketExists() {
  79. return $this->s3->if_bucket_exists($this->bucket);
  80. }
  81. /**
  82. * Attempt to create the bucket for the given region
  83. *
  84. * @param string $region
  85. * @return bool
  86. */
  87. public function createBucket($region = AmazonS3::REGION_US_W1) {
  88. $response = $this->s3->create_bucket($this->bucket,$region);
  89. return $response->isOK() ? true : false;
  90. }
  91. /**
  92. * Upload a single item to S3
  93. *
  94. * @param array $file The PHP FILE array for the file
  95. * @param string $target The relative path in the bucket in which to place the file
  96. * @param array $options An array of options for uploading to S3
  97. * @return bool|string
  98. */
  99. public function uploadSingle($file,$target,array $options = array()) {
  100. $options = array_merge(array(
  101. 'acl' => AmazonS3::ACL_PUBLIC,
  102. ),$options);
  103. if (is_array($file)) {
  104. $filename = basename($file['name']);
  105. $file = $file['tmp_name'];
  106. } else {
  107. $filename = basename($file);
  108. }
  109. $options['fileUpload'] = $file;
  110. $response = $this->s3->create_object($this->bucket,$target.$filename,$options);
  111. if ($response->status != 200) {
  112. return false;
  113. }
  114. return $this->s3->get_object_url($this->bucket,$target.$filename);
  115. }
  116. /**
  117. * Upload an array of files to S3
  118. *
  119. * @param $files The PHP $_FILES array which references the files to upload
  120. * @param string $target The relative path in the S3 bucket to upload to
  121. * @param array $options An array of options to pass to the S3 upload client
  122. * @return array|bool If ok, will return an array of URLs for the uploaded items
  123. */
  124. public function upload($files,$target = '',array $options = array()) {
  125. if (!is_array($files) || !empty($files['tmp_name'])) $files = array($files);
  126. $options = array_merge(array(
  127. 'acl' => AmazonS3::ACL_PUBLIC,
  128. ),$options);
  129. $individualFiles = array();
  130. foreach ($files as $k => $file) {
  131. if (is_array($file)) {
  132. $filename = basename($file['name']);
  133. $file = $file['tmp_name'];
  134. } else {
  135. $filename = basename($file);
  136. }
  137. $individualFiles[] = $filename;
  138. $options['fileUpload'] = $file;
  139. $this->s3->batch()->create_object($this->bucket,$target.$filename,$options);
  140. }
  141. $response = $this->s3->batch()->send();
  142. if ($response->areOK()) {
  143. $data = array();
  144. foreach ($individualFiles as $filename) {
  145. $data[] = $this->s3->get_object_url($this->bucket,$target.$filename);
  146. }
  147. return $data;
  148. }
  149. return false;
  150. }
  151. /**
  152. * Get the S3 absolute URL for the specified path
  153. *
  154. * @param string $path The path by which to grab the URL
  155. * @param string $expires The time in which the URL expires
  156. * @return bool|mixed|string
  157. */
  158. public function getFileUrl($path,$expires = null) {
  159. return $this->s3->get_object_url($this->bucket,$path,$expires);
  160. }
  161. }