awsfileserver.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /****************************
  3. * File : awsfileserver.php
  4. * Author : Cygnusa
  5. * @2018
  6. ****************************/
  7. //SECURITY
  8. if (!defined('APPLICATION_PATH')) {
  9. exit('No direct script access allowed');
  10. }
  11. //AMAZON S3
  12. require dirname(__FILE__) . '/_api/awsapi/aws-autoloader.php';
  13. use Aws\Exception\AwsException;
  14. use Aws\S3\S3Client;
  15. //CLASS DEFINE
  16. class awsfileserver
  17. {
  18. //FUNCTION LISTS
  19. public function __construct()
  20. {
  21. //return connectS3();
  22. }
  23. /* ////////////////////////////////////// S3 CONNECT ///////////////////////////////// */
  24. public static function connectS3()
  25. {
  26. //S3 CONNECTION
  27. $s3 = S3Client::factory(array(
  28. 'version' => 'latest',
  29. 'region' => S3_REGION,
  30. 'credentials' => array(
  31. 'key' => S3_ACCESSKEY,
  32. 'secret' => S3_SECRET_ACCESS,
  33. ),
  34. ));
  35. return $s3;
  36. }
  37. /* ////////////////////////////////////// S3 URL ///////////////////////////////// */
  38. public static function getS3Url($s3, $bucket, $obj)
  39. {
  40. $presignedUrl = '';
  41. if (is_object($obj)) {
  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. /* ///////////////////////////////// EXIST ///////////////////////////////////// */
  52. public static function checkexist($s3, $bucket, $obj)
  53. {
  54. $info = $s3->doesObjectExist($bucket, $obj->key_name);
  55. return $info;
  56. }
  57. /* ///////////////////////////////// UPLOAD /////////////////////////////////// */
  58. public static function checkupload($s3, $bucket, $upload_key, $s3upload_key)
  59. {
  60. try {
  61. $params = array(
  62. 'Bucket' => $bucket,
  63. 'Key' => $upload_key,
  64. 'SourceFile' => $s3upload_key,
  65. );
  66. // Using commands explicitly.
  67. $command = $s3->getCommand('PutObject', $params);
  68. $result = $s3->execute($command);
  69. } catch (AwsException $e) {
  70. self::errormsg('S3 SERVER ERROR' . $e);
  71. }
  72. }
  73. /* ////////////////////////////////////////////////////////////////////////////// */
  74. public static function deleteobject($s3, $bucket, $s3upload_key)
  75. {
  76. //$keyfile_name="uploads/noimage.jpg";
  77. try {
  78. $result = $s3->deleteObject(array(
  79. 'Bucket' => $bucket,
  80. 'Key' => $s3upload_key,
  81. ));
  82. // print_r($result);
  83. return true;
  84. } catch (AwsException $e) {
  85. self::errormsg('S3 SERVER ERROR' . $e);
  86. }
  87. }
  88. public static function errormsg($msg)
  89. {
  90. $obj = new stdClass();
  91. $obj->mess = $msg;
  92. $obj->cont = false;
  93. echo json_encode($obj);exit;
  94. }
  95. }