mise en place de minio
This commit is contained in:
238
src/Service/MinioService.php
Normal file
238
src/Service/MinioService.php
Normal file
@ -0,0 +1,238 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use Aws\S3\Exception\S3Exception;
|
||||
use Exception;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
|
||||
|
||||
class MinioService
|
||||
{
|
||||
const ERR_UNAVAILABLE = 'Service de gestion de fichiers momentanément indisponible.';
|
||||
const ERR_FILE_NOT_FOUND = 'messages.minio.404';
|
||||
protected $client;
|
||||
protected $listClient;
|
||||
protected $minioBucket;
|
||||
protected $minioRoot;
|
||||
protected $minioPathStyle;
|
||||
protected $minioSecure;
|
||||
|
||||
public function __construct($rootPath, $minioUrl, $minioKey, $minioSecret, $minioBucket, $minioRoot, $minioPathstyle, $minioSecure)
|
||||
{
|
||||
$this->rootPath = $rootPath;
|
||||
$this->minioBucket = $minioBucket;
|
||||
$this->minioPathStyle = ($minioPathstyle==1?true:false);
|
||||
$this->minioRoot = $minioRoot;
|
||||
$this->client = $this->getClient($minioUrl, $minioKey, $minioSecret, $minioPathstyle, $minioSecure);
|
||||
$this->initBucket();
|
||||
}
|
||||
|
||||
public function download(string $file, string $filename, bool $returnFile = false)
|
||||
{
|
||||
// On s'assure que le repertoire temporaire de destination existe bien
|
||||
$fs = new Filesystem();
|
||||
$tmpdir=$this->rootPath."/var/tmp";
|
||||
$fs->mkdir($tmpdir."/".dirname($filename));
|
||||
|
||||
// Approche repassant par le serveur d'appel
|
||||
try {
|
||||
$result = $this->client->getObject([
|
||||
'Bucket' => $this->minioBucket,
|
||||
'Key' => $this->minioRoot.$file,
|
||||
'SaveAs' => $tmpdir.'/'.$filename,
|
||||
]);
|
||||
} catch (S3Exception $e) {
|
||||
dump($e);
|
||||
switch ($e->getResponse()->getStatusCode()) {
|
||||
case 404:
|
||||
throw new NotFoundHttpException($this->translator->trans(self::ERR_FILE_NOT_FOUND, [], 'messages'));
|
||||
break;
|
||||
default:
|
||||
\Sentry\captureException($e);
|
||||
throw new Exception(self::ERR_UNAVAILABLE);
|
||||
break;
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
\Sentry\captureException($e);
|
||||
throw new Exception(self::ERR_UNAVAILABLE);
|
||||
}
|
||||
|
||||
if ($returnFile) {
|
||||
return $tmpdir.'/'.$filename;
|
||||
} else {
|
||||
//Suppression de la copie locale
|
||||
unlink($tmpdir.'/'.$filename);
|
||||
$res = new Response($result['Body'], 200);
|
||||
$res->headers->set('Content-Type', 'application/pdf');
|
||||
$res->headers->set('Content-Description', 'File Transfer');
|
||||
$res->headers->set('Content-Disposition', 'attachment; filename='.$filename);
|
||||
$res->headers->set('Expires', '0');
|
||||
$res->headers->set('Cache-Control', 'must-revalidate');
|
||||
$res->headers->set('Pragma', 'public');
|
||||
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
|
||||
public function upload($file, $filename, $deleteSource = false)
|
||||
{
|
||||
try {
|
||||
$this->client->putObject([
|
||||
'Bucket' => $this->minioBucket,
|
||||
'Key' => $this->minioRoot.$filename,
|
||||
'SourceFile' => $file,
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
\Sentry\captureException($e);
|
||||
throw new Exception(self::ERR_UNAVAILABLE);
|
||||
}
|
||||
|
||||
if ($deleteSource) {
|
||||
unlink($file);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* move.
|
||||
*
|
||||
* @param bool $deleteSource
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function move(string $from, string $to, $deleteSource = false)
|
||||
{
|
||||
try {
|
||||
$this->client->copyObject([
|
||||
'Bucket' => $this->minioBucket,
|
||||
'Key' => $this->minioRoot.$to,
|
||||
'CopySource' => $this->minioBucket.'/'.$this->minioRoot.$from,
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
\Sentry\captureException($e);
|
||||
dump($this->minioRoot.$to);
|
||||
dump($e->getMessage());
|
||||
throw new Exception(self::ERR_UNAVAILABLE);
|
||||
}
|
||||
|
||||
if ($deleteSource) {
|
||||
$this->delete($from);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* delete.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function delete(string $file)
|
||||
{
|
||||
try {
|
||||
$this->client->deleteObject([
|
||||
'Bucket' => $this->minioBucket,
|
||||
'Key' => $this->minioRoot.$file,
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
\Sentry\captureException($e);
|
||||
throw new Exception(self::ERR_UNAVAILABLE);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* countKeys.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function countKeys(string $prefix)
|
||||
{
|
||||
//On utilise un path spécifique car listObjectsV2 utilise une autre config de client
|
||||
try {
|
||||
$response = $this->client->listObjectsV2([
|
||||
'Bucket' => $this->minioBucket,
|
||||
'Prefix' => $prefix,
|
||||
]);
|
||||
|
||||
return $response->get('KeyCount');
|
||||
} catch (Exception $e) {
|
||||
\Sentry\captureException($e);
|
||||
throw new Exception(self::ERR_UNAVAILABLE);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* listKeys.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function listKeys(string $prefix)
|
||||
{
|
||||
//On utilise un path spécifique car listObjectsV2 utilise une autre config de client
|
||||
try {
|
||||
$response = $this->client->listObjectsV2([
|
||||
'Bucket' => $this->minioBucket,
|
||||
'Prefix' => $prefix,
|
||||
]);
|
||||
|
||||
return $response->get('Contents');
|
||||
} catch (Exception $e) {
|
||||
\Sentry\captureException($e);
|
||||
throw new Exception(self::ERR_UNAVAILABLE);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* download.
|
||||
*
|
||||
* @param string $file Nom du fichier dans minio
|
||||
* @param string $filename Nom du fichier dans la réponse
|
||||
* @param bool $returnFile Retourner un fichier ou une réponse
|
||||
*/
|
||||
|
||||
|
||||
protected function getClient($minioUrl, $minioKey, $minioSecret, bool $minioPathstyle, bool $minioSecure)
|
||||
{
|
||||
$client = new \Aws\S3\S3Client([
|
||||
'version' => 'latest',
|
||||
'region' => 'eu-west-1',
|
||||
'endpoint' => $minioUrl,
|
||||
//On force le mode DNS
|
||||
'use_path_style_endpoint' => $minioPathstyle,
|
||||
'credentials' => [
|
||||
'key' => $minioKey,
|
||||
'secret' => $minioSecret,
|
||||
],
|
||||
//On désactive les checks SSL pour le moment
|
||||
'http' => [
|
||||
'verify' => $minioSecure,
|
||||
],
|
||||
]);
|
||||
|
||||
return $client;
|
||||
}
|
||||
|
||||
protected function initBucket()
|
||||
{
|
||||
try {
|
||||
$bucketExists = false;
|
||||
|
||||
$buckets = $this->client->listBuckets()->toArray()['Buckets'];
|
||||
|
||||
foreach ($buckets as $bucket) {
|
||||
if ($this->minioBucket == $bucket['Name']) {
|
||||
$bucketExists = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$bucketExists) {
|
||||
$this->client->createBucket([
|
||||
'Bucket' => $this->minioBucket,
|
||||
]);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
dump($e->getMessage());
|
||||
throw new Exception(self::ERR_UNAVAILABLE);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user