Advertisement
jimgreeno

CHMOD 77)

Sep 16th, 2017
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. /**
  4.  * S3 Upload component for Yii2 framework
  5.  *
  6.  * S3 is a wrapper for AWS SDK for PHP (@link https://github.com/aws/aws-sdk-php)
  7.  * This wrapper contains minimal functionality as there is only so much I want to allow access to from the Yii public end
  8.  *
  9.  * @version 0.1
  10.  *
  11.  * @author Maxim Gordienko (3dmaxpayne.com)
  12.  */
  13.  
  14. namespace yiimag;
  15.  
  16. use Yii;
  17.  
  18. use AwsS3S3Client;
  19. use AwsS3ExceptionS3Exception;
  20. use yiibaseComponent;
  21. use yiibaseException;
  22. use yiimagS3FileUpload;
  23.  
  24. class S3 extends Component
  25. {
  26.  
  27.     private $_s3;
  28.     public $key;    // AWS Access key
  29.     public $secret; // AWS Secret key
  30.     public $bucket;
  31.      public $region;
  32.        public $version='2006-03-01';
  33.     public $lastError="";
  34.   private $_folder        = '######';
  35.     private $_treeStructure = true;
  36.     private $_hashFilename  = true;
  37.     private $_fsPath        = '########';
  38.     private $_fsUrl         = '##############';
  39.     private $_ACL           = '########';
  40.     /**
  41.      * Return S3 Client instance
  42.      * @return S3Client
  43.      */
  44.     private function getInstance()
  45.     {
  46.         if ($this->_s3 === NULL){
  47.         $this->_s3=$this->connect();}
  48.         return $this->_s3;
  49.     }
  50.  
  51.     /**
  52.      * Instance the S3 object
  53.      */
  54.     public function connect()
  55.     {
  56.         if ( $this->key === NULL || $this->secret === NULL )
  57.             throw new Exception('S3 Keys are not set.');
  58.  
  59.         $this->_s3 = S3Client::factory([
  60.            'credentials' => ['key' => $this->key, 'secret' => $this->secret],  //'key'    => ,
  61.             //'secret' => ,
  62.             'version' => $this->version,
  63.             'region'=>$this->region,
  64.             'bucket'=>$this->bucket,
  65.         ]);
  66.  
  67.         return  $this->_s3 ;
  68.     }
  69.  
  70.     /**
  71.      * Upload file to S3
  72.      * @param string $file path to file on local server
  73.      * @param string $fileName name of file on Amazon. It can include directories.
  74.      * @param null $bucket bucket name. By default use bucket from config
  75.      */
  76.     public function upload($file, $fileName, $bucket)
  77.     {
  78.  
  79.             $bucket = $this->bucket;
  80.  
  81.  
  82.          //$aws = Yii::$app->awssdk->getAwsSdk();
  83.      //$s3 =new S3 ;
  84.              //$s3=$s3->createS3();
  85.  
  86.         if(!empty($fileName)){
  87.         try {
  88.             /*$s3->putObject(array(
  89.                 'Bucket' => $bucket,
  90.                 //'SourceFile' => $fileName,
  91.                 'Key'    => basename($file),
  92.                 'Body'   => $file,
  93.                 'SourceFile'   => $file->tempName,
  94.              'ContentType'  => $file->type,
  95.                 'ACL'    => 'public-read',
  96.                 'StorageClass' => 'REDUCED_REDUNDANCY',
  97.  
  98.             ));*/
  99.  
  100.               $s3 = $this->getInstance();
  101.               $upload= $s3->putObject(
  102.             array(
  103.              'Bucket'       => $this->bucket,
  104.              'Key'          => $this->getUploadFolder().$this->getFilename($fileName),
  105.              'SourceFile'   => $fileName->tempName,
  106.              'ContentType'  => $fileName->type,
  107.              'ACL'          => $this->_ACL,
  108.              'StorageClass' => 'REDUCED_REDUNDANCY',
  109.             )
  110.         );
  111.         //var_dump($upload);die;
  112.         //return $upload->get(['ObjectURL']);
  113.  
  114.  
  115.             //return 1;//$upload['ObjectURL'];
  116.  
  117.            /* $photo = UploadedFile::getInstance($file, $fileName);
  118. $uploader = new FileUpload(FileUpload::S_S3, [
  119.     'version' => $this->version,
  120.     'region' => $this->region,
  121.     'credentials' => [
  122.         'key' => $this->key,
  123.         'secret' => $this->secret
  124.     ],
  125.     'bucket' => $this->bucket
  126. ]);*/
  127.         //return 1;
  128.         } catch (S3Exception $e) {
  129.  
  130.             return 0;
  131.             //echo "There was an error uploading the file.n";
  132.         }}else{return 0;}
  133.     }
  134.  
  135.     /**
  136.      * Use for call another functions of S3Client
  137.      * @param string $func
  138.      * @param array $args
  139.      * @return mixed
  140.      * @throws yiibaseException
  141.      */
  142.       final function getTreeStructureMap()
  143.     {
  144.         return substr(md5(microtime()), mt_rand(0, 30), 2).DIRECTORY_SEPARATOR.substr(md5(microtime()), mt_rand(0, 30), 2);
  145.  
  146.     }
  147.     final function getUploadFolder()
  148.     {
  149.         return $this->_folder.'/';
  150.  
  151.     }
  152.  
  153.     final function getFilename($file){
  154.  
  155.  
  156.  
  157.         //$fileName  = $file->name;
  158.         $fileName= str_replace([" ", "."], "_", $file->name).date('_Y_m_d_H_i').'.'.$file->extension;
  159.         $pathParts = pathinfo($fileName);
  160.  
  161.         return  $fileName;
  162.  
  163.     }
  164.  
  165.     public function __call($func, $args )
  166.     {
  167.         $s3 = $this->getInstance();
  168.         return call_user_func([$s3, $func], $args[0]);
  169.     }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement