Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * S3 Upload component for Yii2 framework
- *
- * S3 is a wrapper for AWS SDK for PHP (@link https://github.com/aws/aws-sdk-php)
- * This wrapper contains minimal functionality as there is only so much I want to allow access to from the Yii public end
- *
- * @version 0.1
- *
- * @author Maxim Gordienko (3dmaxpayne.com)
- */
- namespace yiimag;
- use Yii;
- use AwsS3S3Client;
- use AwsS3ExceptionS3Exception;
- use yiibaseComponent;
- use yiibaseException;
- use yiimagS3FileUpload;
- class S3 extends Component
- {
- private $_s3;
- public $key; // AWS Access key
- public $secret; // AWS Secret key
- public $bucket;
- public $region;
- public $version='2006-03-01';
- public $lastError="";
- private $_folder = '######';
- private $_treeStructure = true;
- private $_hashFilename = true;
- private $_fsPath = '########';
- private $_fsUrl = '##############';
- private $_ACL = '########';
- /**
- * Return S3 Client instance
- * @return S3Client
- */
- private function getInstance()
- {
- if ($this->_s3 === NULL){
- $this->_s3=$this->connect();}
- return $this->_s3;
- }
- /**
- * Instance the S3 object
- */
- public function connect()
- {
- if ( $this->key === NULL || $this->secret === NULL )
- throw new Exception('S3 Keys are not set.');
- $this->_s3 = S3Client::factory([
- 'credentials' => ['key' => $this->key, 'secret' => $this->secret], //'key' => ,
- //'secret' => ,
- 'version' => $this->version,
- 'region'=>$this->region,
- 'bucket'=>$this->bucket,
- ]);
- return $this->_s3 ;
- }
- /**
- * Upload file to S3
- * @param string $file path to file on local server
- * @param string $fileName name of file on Amazon. It can include directories.
- * @param null $bucket bucket name. By default use bucket from config
- */
- public function upload($file, $fileName, $bucket)
- {
- $bucket = $this->bucket;
- //$aws = Yii::$app->awssdk->getAwsSdk();
- //$s3 =new S3 ;
- //$s3=$s3->createS3();
- if(!empty($fileName)){
- try {
- /*$s3->putObject(array(
- 'Bucket' => $bucket,
- //'SourceFile' => $fileName,
- 'Key' => basename($file),
- 'Body' => $file,
- 'SourceFile' => $file->tempName,
- 'ContentType' => $file->type,
- 'ACL' => 'public-read',
- 'StorageClass' => 'REDUCED_REDUNDANCY',
- ));*/
- $s3 = $this->getInstance();
- $upload= $s3->putObject(
- array(
- 'Bucket' => $this->bucket,
- 'Key' => $this->getUploadFolder().$this->getFilename($fileName),
- 'SourceFile' => $fileName->tempName,
- 'ContentType' => $fileName->type,
- 'ACL' => $this->_ACL,
- 'StorageClass' => 'REDUCED_REDUNDANCY',
- )
- );
- //var_dump($upload);die;
- //return $upload->get(['ObjectURL']);
- //return 1;//$upload['ObjectURL'];
- /* $photo = UploadedFile::getInstance($file, $fileName);
- $uploader = new FileUpload(FileUpload::S_S3, [
- 'version' => $this->version,
- 'region' => $this->region,
- 'credentials' => [
- 'key' => $this->key,
- 'secret' => $this->secret
- ],
- 'bucket' => $this->bucket
- ]);*/
- //return 1;
- } catch (S3Exception $e) {
- return 0;
- //echo "There was an error uploading the file.n";
- }}else{return 0;}
- }
- /**
- * Use for call another functions of S3Client
- * @param string $func
- * @param array $args
- * @return mixed
- * @throws yiibaseException
- */
- final function getTreeStructureMap()
- {
- return substr(md5(microtime()), mt_rand(0, 30), 2).DIRECTORY_SEPARATOR.substr(md5(microtime()), mt_rand(0, 30), 2);
- }
- final function getUploadFolder()
- {
- return $this->_folder.'/';
- }
- final function getFilename($file){
- //$fileName = $file->name;
- $fileName= str_replace([" ", "."], "_", $file->name).date('_Y_m_d_H_i').'.'.$file->extension;
- $pathParts = pathinfo($fileName);
- return $fileName;
- }
- public function __call($func, $args )
- {
- $s3 = $this->getInstance();
- return call_user_func([$s3, $func], $args[0]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement