awsfileserver.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /****************************
  3. * File : awsfileserver.php
  4. * Author : Cygnusa
  5. * @2018
  6. ****************************/
  7. //SECURITY
  8. if ( ! defined('APPLICATION_PATH')) exit('No direct script access allowed');
  9. //AMAZON S3
  10. require (dirname(__FILE__).'/../awsapi/aws-autoloader.php');
  11. use Aws\S3\S3Client;
  12. use Aws\Exception\AwsException;
  13. //CLASS DEFINE
  14. class awsfileserver
  15. {
  16. //FUNCTION LISTS
  17. function __construct($parent)
  18. {
  19. //return connectS3();
  20. }
  21. /* ////////////////////////////////////// S3 CONNECT ///////////////////////////////// */
  22. public static function connectS3()
  23. {
  24. //S3 CONNECTION
  25. $s3 = S3Client::factory(array(
  26. 'version' => 'latest',
  27. 'region' => S3_REGION,
  28. 'credentials' => array(
  29. 'key' =>S3_ACCESSKEY,
  30. 'secret' =>S3_SECRET_ACCESS
  31. )
  32. ));
  33. return $s3;
  34. }
  35. /* ///////////////////////////////////////////////////////////////////////////////////// */
  36. /* ////////////////////////////////////// S3 URL ///////////////////////////////// */
  37. public static function getS3Url($s3,$bucket,$obj)
  38. {
  39. $presignedUrl='';
  40. if(is_object($obj))
  41. {
  42. $cmd = $s3->getCommand('GetObject', [
  43. 'Bucket' =>$bucket,
  44. 'Key' => $obj->key_name
  45. ]);
  46. $request = $s3->createPresignedRequest($cmd, '+7200 minutes');
  47. $presignedUrl = (string) $request->getUri();
  48. }
  49. return $presignedUrl;
  50. }
  51. /* ///////////////////////////////////////////////////////////////////////////////////// */
  52. /* ////////////////////////////////////// S3 URL ///////////////////////////////// */
  53. public static function getS3FileUrl($s3,$bucket,$key_name)
  54. {
  55. $presignedUrl='';
  56. if(isset($key_name) && $key_name!='')
  57. {
  58. $cmd = $s3->getCommand('GetObject', [
  59. 'Bucket' =>$bucket,
  60. 'Key' => $key_name
  61. ]);
  62. $request = $s3->createPresignedRequest($cmd, '+7200 minutes');
  63. $presignedUrl = (string) $request->getUri();
  64. }
  65. return $presignedUrl;
  66. }
  67. /* ///////////////////////////////////////////////////////////////////////////////////// */
  68. /* ///////////////////////////////// EXIST ///////////////////////////////////// */
  69. public static function checkexist($s3,$bucket,$obj)
  70. {
  71. $info = $s3->doesObjectExist($bucket,$obj->key_name);
  72. return $info;
  73. }
  74. /* ///////////////////////////////////////////////////////////////////////////////////// */
  75. /* ///////////////////////////////// UPLOAD /////////////////////////////////// */
  76. public static function checkupload($parent,$s3,$bucket,$upload_key,$s3upload_key)
  77. {
  78. //$keyfile_name="uploads/noimage.jpg";
  79. try {
  80. $params = array(
  81. 'Bucket' =>$bucket,
  82. 'Key' =>$upload_key,
  83. 'SourceFile' =>$s3upload_key
  84. );
  85. // Using commands explicitly.
  86. $command = $s3->getCommand('PutObject', $params);
  87. $result = $s3->execute($command);
  88. } catch (AwsException $e) {
  89. // output error message if fails
  90. //echo $e->getMessage();
  91. $parent->response(array('response'=>'failed','message'=>'S3 SERVER ERROR'),500);
  92. }
  93. }
  94. /* ////////////////////////////////////////////////////////////////////////////// */
  95. /* ///////////////////////////////// DELETE /////////////////////////////////// */
  96. public static function deleteobject($parent,$s3,$bucket,$s3upload_key)
  97. {
  98. //$keyfile_name="uploads/noimage.jpg";
  99. try {
  100. $result = $s3->deleteObject(array(
  101. 'Bucket' => $bucket,
  102. 'Key' => $s3upload_key
  103. ));
  104. return true;
  105. } catch (AwsException $e) {
  106. // output error message if fails
  107. //echo $e->getMessage();
  108. $parent->response(array('response'=>'failed','message'=>'S3 SERVER ERROR'),500);
  109. }
  110. }
  111. /* ////////////////////////////////////////////////////////////////////////////// */
  112. /* ///////////////////////////////// MOVE TO SE TO LOCAL /////////////////////////////////// */
  113. public static function movefiletolocal($parent,$s3,$bucket,$data)
  114. {
  115. //data
  116. try {
  117. //$data=array(["s3"=>"s3 path","local"=>"local path"],["s3 path","local"=>"local path"],....));
  118. //$arr=[];
  119. if(is_array($data) && count($data)>0)
  120. {
  121. foreach($data as $val)
  122. {
  123. if(!(is_array($val) && array_key_exists("s3",$val) && array_key_exists("local",$val))) $parent->response(array('response'=>'failed','message'=>'INVALID DATA IN - awsfiledsever.php "movefiletolocal"'),500);
  124. $object = $s3->getObject(['Bucket' => $bucket, 'Key' => $val["s3"],'SaveAs'=>$val["local"]]);
  125. }
  126. //print_r($arr);
  127. }else
  128. {
  129. $parent->response(array('response'=>'failed','message'=>'INVALID DATA IN - awsfiledsever.php "movefiletolocal"'),500);
  130. }
  131. } catch (AwsException $e) {
  132. // output error message if fails
  133. //echo $e->getMessage();
  134. $parent->response(array('response'=>'failed','message'=>'S3 SERVER ERROR'),500);
  135. }
  136. }
  137. /* ////////////////////////////////////////////////////////////////////////////// */
  138. }
  139. ?>