PageRenderTime 22ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/api/libs/S3.php

https://gitlab.com/x33n/respond
PHP | 460 lines | 259 code | 114 blank | 87 comment | 16 complexity | 3eb98322f426f4d3a1e679cc6f9b276d MD5 | raw file
  1. <?php
  2. // Wrapper for Amazon S3 utilities
  3. class S3
  4. {
  5. // creates a bucket on S3
  6. public static function CreateBucket($bucket){
  7. // create AWS client
  8. $client = Aws\S3\S3Client::factory(array(
  9. 'key' => S3_KEY,
  10. 'secret' => S3_SECRET,
  11. 'region' => S3_LOCATION
  12. ));
  13. // check to see if bucket exists
  14. $doesExist = $client->doesBucketExist($bucket);
  15. // create a bucket for the site if it does not exists
  16. if($doesExist == false){
  17. // #ref: http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.S3.S3Client.html#_createBucket
  18. $result = $client->createBucket(array(
  19. 'Bucket' => $bucket,
  20. 'ACL' => 'public-read'
  21. ));
  22. // enable hosting for the bucket
  23. $result = $client->putBucketWebsite(array(
  24. // Bucket is required
  25. 'Bucket' => $bucket,
  26. 'ErrorDocument' => array(
  27. // Key is required
  28. 'Key' => '#/page/error',
  29. ),
  30. 'IndexDocument' => array(
  31. // Suffix is required
  32. 'Suffix' => 'index.html',
  33. )));
  34. }
  35. return true;
  36. }
  37. // saves a file to S3
  38. public static function SaveFile($site, $contentType, $filename, $file, $meta = array(), $folder = 'files'){
  39. // create AWS client
  40. $client = Aws\S3\S3Client::factory(array(
  41. 'key' => S3_KEY,
  42. 'secret' => S3_SECRET,
  43. 'region' => S3_LOCATION
  44. ));
  45. $bucket = $site['Bucket'];
  46. // create a bucket if it doesn't already exist
  47. S3::CreateBucket($bucket);
  48. $result = $client->putObject(array(
  49. 'Bucket' => $bucket,
  50. 'Key' => $site['FriendlyId'].'/'.$folder.'/'.$filename,
  51. 'Body' => file_get_contents($file),
  52. 'ContentType' => $contentType,
  53. 'ACL' => 'public-read',
  54. 'StorageClass' => 'REDUCED_REDUNDANCY',
  55. 'Metadata' => $meta
  56. ));
  57. }
  58. // removes a file from S3
  59. public static function RemoveFile($site, $filename, $folder = 'files'){
  60. // create AWS client
  61. $client = Aws\S3\S3Client::factory(array(
  62. 'key' => S3_KEY,
  63. 'secret' => S3_SECRET,
  64. 'region' => S3_LOCATION
  65. ));
  66. $bucket = $site['Bucket'];
  67. // remove file
  68. $result = $client->deleteObject(array(
  69. 'Bucket' => $bucket,
  70. 'Key' => $site['FriendlyId'].'/'.$folder.'/'.$filename
  71. ));
  72. // remove thumb
  73. $result = $client->deleteObject(array(
  74. 'Bucket' => $bucket,
  75. 'Key' => $site['FriendlyId'].'/'.$folder.'/thumbs/'.$filename
  76. ));
  77. }
  78. // removes a site from S3
  79. public static function RemoveSite($site){
  80. // create AWS client
  81. $client = Aws\S3\S3Client::factory(array(
  82. 'key' => S3_KEY,
  83. 'secret' => S3_SECRET,
  84. 'region' => S3_LOCATION
  85. ));
  86. $bucket = $site['Bucket'];
  87. // iterate through bucket
  88. $objects = $client->getIterator('ListObjects', array(
  89. 'Bucket' => $bucket,
  90. 'Prefix' => $site['FriendlyId'].'/'
  91. ));
  92. // walk through objects
  93. foreach ($objects as $object) {
  94. $key = $object['Key'];
  95. $source = $bucket.'/'.$key;
  96. $dest = str_replace($site['FriendlyId'].'/', 'removed-'.$site['FriendlyId'].'/', $key);
  97. // copy object (rename it)
  98. $result = $client->copyObject(array(
  99. 'Bucket' => $bucket,
  100. 'CopySource' => $source,
  101. 'Key' => $dest,
  102. ));
  103. // remove object
  104. $result = $client->deleteObject(array(
  105. 'Bucket' => $bucket,
  106. 'Key' => $key
  107. ));
  108. }
  109. /*
  110. $clear = new Aws\S3\Model\ClearBucket($s3Client, $bucket);
  111. // Be sure to set the custom iterator to ensure that you only delete keys with the prefix
  112. $clear->setIterator($iterator);
  113. // Clear out the matching objects using batches in parallel
  114. $clear->clear(); */
  115. }
  116. // get file
  117. public static function GetFile($site, $filename, $folder = 'files'){
  118. // create AWS client
  119. $client = Aws\S3\S3Client::factory(array(
  120. 'key' => S3_KEY,
  121. 'secret' => S3_SECRET,
  122. 'region' => S3_LOCATION
  123. ));
  124. $bucket = $site['Bucket'];
  125. // get object
  126. $result = $client->getObject(array(
  127. 'Bucket' => $bucket,
  128. 'Key' => $site['FriendlyId'].'/'.$folder.'/'.$filename
  129. ));
  130. return $result;
  131. }
  132. // saves contents to S3
  133. public static function SaveContents($site, $contentType, $filename, $contents, $meta = array(), $folder = 'files'){
  134. // create AWS client
  135. $client = Aws\S3\S3Client::factory(array(
  136. 'key' => S3_KEY,
  137. 'secret' => S3_SECRET,
  138. 'region' => S3_LOCATION
  139. ));
  140. $bucket = $site['Bucket'];
  141. // create a bucket if it doesn't already exist
  142. S3::CreateBucket($bucket);
  143. $result = $client->putObject(array(
  144. 'Bucket' => $bucket,
  145. 'Key' => $site['FriendlyId'].'/'.$folder.'/'.$filename,
  146. 'Body' => $contents,
  147. 'ContentType' => $contentType,
  148. 'ACL' => 'public-read',
  149. 'StorageClass' => 'REDUCED_REDUNDANCY',
  150. 'Metadata' => $meta
  151. ));
  152. }
  153. // lists files on S3
  154. public static function ListFiles($site, $imagesOnly = false, $folder = 'files'){
  155. $arr = array();
  156. // create AWS client
  157. $client = Aws\S3\S3Client::factory(array(
  158. 'key' => S3_KEY,
  159. 'secret' => S3_SECRET,
  160. 'region' => S3_LOCATION
  161. ));
  162. $bucket = $site['Bucket'];
  163. $prefix = $site['FriendlyId'].'/'.$folder.'/';
  164. $url = str_replace('{{bucket}}', $site['Bucket'], S3_URL);
  165. $url = str_replace('{{site}}', $site['FriendlyId'], $url);
  166. // list objects in a bucket
  167. $iterator = $client->getIterator('ListObjects', array(
  168. 'Bucket' => $bucket,
  169. 'Prefix' => $prefix
  170. ));
  171. foreach ($iterator as $object) {
  172. $filename = $object['Key'];
  173. $size = $object['Size'];
  174. $headers = $client->headObject(array(
  175. 'Bucket' => $bucket,
  176. 'Key' => $object['Key']
  177. ));
  178. $headers = $headers->toArray();
  179. // defaults
  180. $width = 0;
  181. $height = 0;
  182. // get width and height from head
  183. if($headers !== NULL){
  184. if(isset($headers['Metadata']['width'])){
  185. $width = $headers['Metadata']['width'];
  186. }
  187. if(isset($headers['Metadata']['height'])){
  188. $height = $headers['Metadata']['height'];
  189. }
  190. }
  191. $filename = str_replace($prefix, '', $filename);
  192. // get extension
  193. $parts = explode(".", $filename);
  194. $ext = end($parts); // get extension
  195. $ext = strtolower($ext); // convert to lowercase
  196. // init
  197. $file = array();
  198. // exclude thumbs and empty directories
  199. if(strpos($filename, 'thumbs/') === FALSE && $filename !== ''){
  200. // determine whether the file is an image
  201. if($ext=='png' || $ext=='jpg' || $ext=='gif' || $ext == 'svg'){ // upload image
  202. $isImage = true;
  203. $file = array(
  204. 'filename' => $filename,
  205. 'fullUrl' => $url.'/'.$folder.'/'.$filename,
  206. 'thumbUrl' => $url.'/'.$folder.'/thumbs/'.$filename,
  207. 'extension' => $ext,
  208. 'isImage' => $isImage,
  209. 'size' => round(($size / 1024 / 1024), 2),
  210. 'width' => $width,
  211. 'height' => $height
  212. );
  213. // push file to array
  214. array_push($arr, $file);
  215. }
  216. else{
  217. // list file if it is allowed
  218. if($imagesOnly == false){
  219. $isImage = false;
  220. $file = array(
  221. 'filename' => $filename,
  222. 'fullUrl' => $url.'/'.$folder.'/'.$filename,
  223. 'thumbUrl' => NULL,
  224. 'extension' => $ext,
  225. 'isImage' => $isImage,
  226. 'size' => $size,
  227. 'width' => NULL,
  228. 'height' => NULL
  229. );
  230. // push file to array
  231. array_push($arr, $file);
  232. }
  233. }
  234. }
  235. }
  236. return $arr;
  237. }
  238. // gets the size in MB of files stored in /file
  239. public static function RetrieveFilesSize($site, $imagesOnly = false){
  240. $arr = array();
  241. // create AWS client
  242. $client = Aws\S3\S3Client::factory(array(
  243. 'key' => S3_KEY,
  244. 'secret' => S3_SECRET,
  245. 'region' => S3_LOCATION
  246. ));
  247. $bucket = $site['Bucket'];
  248. $prefix = $site['FriendlyId'].'/files/';
  249. // list objects in a bucket
  250. $iterator = $client->getIterator('ListObjects', array(
  251. 'Bucket' => $bucket,
  252. 'Prefix' => $prefix
  253. ));
  254. // totalsize
  255. $total_size = 0;
  256. // walk through objects
  257. foreach ($iterator as $object) {
  258. $filename = $object['Key'];
  259. $size = $object['Size'];
  260. $filename = str_replace($prefix, '', $filename);
  261. // init
  262. $file = array();
  263. // exclude thumbs and empty directories
  264. if(strpos($filename, 'thumbs/') === FALSE && $filename !== ''){
  265. $total_size += $size;
  266. }
  267. }
  268. return round(($total_size / 1024 / 1024), 2);
  269. }
  270. // deploys the site to Amazon S3
  271. public static function DeploySite($siteId){
  272. // get a reference to the site
  273. $site = Site::GetBySiteId($siteId);
  274. // create AWS client
  275. $client = Aws\S3\S3Client::factory(array(
  276. 'key' => S3_KEY,
  277. 'secret' => S3_SECRET,
  278. 'region' => S3_LOCATION
  279. ));
  280. $bucket = $site['Bucket'];
  281. $bucket_www = 'www.'.$site['Bucket'];
  282. // create a bucket if it doesn't already exist
  283. S3::CreateBucket($bucket);
  284. // set local director
  285. $local_dir = SITES_LOCATION.'/'.$site['FriendlyId'];
  286. // prefix
  287. $keyPrefix = '';
  288. // set permissions
  289. $options = array(
  290. 'params' => array('ACL' => 'public-read'),
  291. 'concurrency' => 20,
  292. 'debug' => true
  293. );
  294. // sync folders, #ref: http://blogs.aws.amazon.com/php/post/Tx2W9JAA7RXVOXA/Syncing-Data-with-Amazon-S3
  295. $client->uploadDirectory($local_dir, $bucket, $keyPrefix, $options);
  296. // get json for the site
  297. $json = json_encode(Publish::CreateSiteJSON($site, 'S3'));
  298. // deploy an updated site.json
  299. $result = $client->putObject(array(
  300. 'Bucket' => $bucket,
  301. 'Key' => 'data/site.json',
  302. 'Body' => $json,
  303. 'ContentType' => 'application/json',
  304. 'ACL' => 'public-read',
  305. 'StorageClass' => 'REDUCED_REDUNDANCY'
  306. ));
  307. /*
  308. // #support for S3 ANAME
  309. // #ref: http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.S3.S3Client.html#_createBucket
  310. $result = $client->createBucket(array(
  311. 'Bucket' => $bucket_www,
  312. 'ACL' => 'public-read'
  313. ));
  314. // enable hosting for the bucket
  315. $result = $client->putBucketWebsite(array(
  316. // Bucket is required
  317. 'Bucket' => $bucket_www,
  318. 'RedirectAllRequestsTo' => array(
  319. 'HostName' => $bucket
  320. )));
  321. */
  322. }
  323. // deploys the directory to Amazon S3
  324. public static function DeployDirectory($site, $local_dir, $keyPrefix){
  325. // create AWS client
  326. $client = Aws\S3\S3Client::factory(array(
  327. 'key' => S3_KEY,
  328. 'secret' => S3_SECRET,
  329. 'region' => S3_LOCATION
  330. ));
  331. $bucket = $site['Bucket'];
  332. // create a bucket if it doesn't already exist
  333. S3::CreateBucket($bucket);
  334. // set permissions
  335. $options = array(
  336. 'params' => array('ACL' => 'public-read'),
  337. 'concurrency' => 20,
  338. 'debug' => true
  339. );
  340. // sync folders, #ref: http://blogs.aws.amazon.com/php/post/Tx2W9JAA7RXVOXA/Syncing-Data-with-Amazon-S3
  341. $client->uploadDirectory($local_dir, $bucket, $site['FriendlyId'].'/'.$keyPrefix, $options);
  342. }
  343. }