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