Created
June 17, 2020 20:49
-
-
Save amansheaven/a4716dd182ba028bae6be07852ea311a to your computer and use it in GitHub Desktop.
Cakephp-Minio Db update component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Controller\Component; | |
use Cake\Controller\Component; | |
use Cake\ORM\TableRegistry; | |
use Aws\S3\S3Client; | |
use Aws\Exception\AwsException; | |
use Cake\Core\Exception\Exception; | |
require 'vendor/autoload.php'; | |
// date_default_timezone_set('America/Los_Angeles'); | |
class UploadComponent extends Component{ | |
private $config = null; | |
private $s3 = null; | |
public function initialize($config){ | |
parent::initialize($config); | |
$this->config = [ | |
's3' => [ | |
'key' => 'accesskeyhere', | |
'secret' => 'secretkeyhere', | |
] | |
]; | |
$this->s3 = S3Client::factory([ | |
'credentials' => [ | |
'key' => $this->config['s3']['key'], | |
'secret' => $this->config['s3']['secret'] | |
], | |
'version' => 'latest', | |
'region' => 'us-east-1', | |
'endpoint' => 'http://minio:9000', | |
'use_path_style_endpoint' => true, | |
]); | |
} | |
private function _save($file,$path,$bucket) | |
{ | |
if(file_exists($path)) | |
{ | |
try { | |
$result = $this->s3->putObject([ | |
'Bucket' => $bucket, | |
'Key' => $file, | |
'SourceFile' => $path | |
]); | |
// $result = $this->s3->createBucket(array('Bucket' => 'mybucket')); | |
// print_r($result); | |
} catch (S3Exception $e) { | |
error_log($e->getMessage() . "\n"); | |
} | |
} | |
else { | |
echo 'error here'; | |
error_log('Failed to put file into mino :: File not found at ' . $file); | |
} | |
} | |
private function _updatedb($user,$file,$path,$bucket) | |
{ | |
$uploadtable = TableRegistry::get('uploads'); | |
$upload = $uploadtable->newEntity(); | |
$upload->uuid = hash('crc32',$file); | |
$upload->filename = $file; | |
$upload->user_id = $user; | |
$upload->sha1 = sha1_file($path); | |
$upload->bucket = $bucket; | |
if($uploadtable->save($upload)) | |
{ | |
return True; | |
} else { | |
return False; | |
} | |
} | |
public function save($user,$file,$path,$bucket) | |
{ | |
if($this->_updatedb($user,$file,$path,$bucket)){ | |
$this->_save($file,$path,$bucket); | |
return True; | |
} else { | |
return False; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment