207 lines
5.5 KiB
PHP
207 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace App\Service;
|
|
|
|
use Aws\S3\S3Client;
|
|
|
|
class AwsService
|
|
{
|
|
|
|
public function __construct(
|
|
private S3Client $s3Client,
|
|
private string $awsPublicUrl
|
|
) {
|
|
|
|
}
|
|
|
|
/**
|
|
* Function to generate UUID Version 4
|
|
*
|
|
* @return string
|
|
*/
|
|
public function generateUUIDv4():string {
|
|
$uuid = uuid_create(4);
|
|
|
|
$isValid = uuid_is_valid($uuid);
|
|
|
|
if( $isValid == true){
|
|
$retour = $uuid;
|
|
}else{
|
|
$retour = 'une erreur est survenue !';
|
|
}
|
|
|
|
return $retour;
|
|
}
|
|
|
|
/**
|
|
* Get public url for files download or visualisation
|
|
*
|
|
* @param string $bucket nom du conteneur S3
|
|
* @return string
|
|
*/
|
|
public function getPublicUrl(string $bucket): string{
|
|
$publicUrl = substr_replace($this->awsPublicUrl, $bucket.'.', 8, 0);
|
|
$publicUrl .= '/';
|
|
|
|
return $publicUrl;
|
|
}
|
|
|
|
/**
|
|
* CREATE bucket S3 for new project
|
|
*
|
|
* @return string|array
|
|
*/
|
|
public function createBucket(): string|array{
|
|
|
|
$bucket = $this->generateUUIDv4();
|
|
|
|
$result = $this->s3Client->createBucket([
|
|
'Bucket' => $bucket,
|
|
'ObjectOwnership' => 'BucketOwnerPreferred'
|
|
]);
|
|
|
|
if ( $result['@metadata']['statusCode'] == 200){
|
|
return $bucket;
|
|
}else{
|
|
return $result['@metadata'];
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* DELETE bucket S3
|
|
*
|
|
* @param string $bucket nom du conteneur S3
|
|
* @return string|array
|
|
*/
|
|
public function DeleteBucket(string $bucket): string|array{
|
|
|
|
$result = $this->s3Client->deleteBucket([
|
|
'Bucket' => $bucket,
|
|
|
|
]);
|
|
|
|
if ( $result['@metadata']['statusCode'] == 200){
|
|
return $bucket;
|
|
}else{
|
|
return $result['@metadata'];
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Get list files infos in the Bucket S3
|
|
* If prefix NULL get ALL FILES else get FILES in this prefix
|
|
*
|
|
* @param string $bucket nom du conteneur S3
|
|
* @param string|null $prefix arborescence dans le bucket
|
|
* @return array|null
|
|
*/
|
|
public function getListObject(string $bucket, string|null $prefix = null):array|null{
|
|
|
|
$results = $this->s3Client->listObjectsV2([
|
|
'Bucket' => $bucket,
|
|
'Prefix' => $prefix
|
|
]);
|
|
|
|
if( isset($results['Contents']) ){
|
|
$return = $results['Contents'];
|
|
}
|
|
|
|
return $return;
|
|
}
|
|
|
|
/**
|
|
* PUT file Object in bucket S3
|
|
*
|
|
* @param string $bucket nom du conteneur S3
|
|
* @param object $file fichier à déposer dans le bucket
|
|
* @param string $filename nom du fichier enregistré dans la bdd métier
|
|
* @param string $mimeType type du fichier
|
|
* @param string|null $prefix arborescence dans le bucket
|
|
* @return bool
|
|
*/
|
|
public function PutDocObj(string $bucket, object $file, string $filename, $mimeType, string|null $prefix = null): int{
|
|
|
|
$body = fopen( $file, 'r');
|
|
$hashRaw = hash_file('sha256', $file, true);
|
|
$hash = base64_encode($hashRaw);
|
|
|
|
rewind($body);
|
|
|
|
$doc = $this->s3Client->putObject([
|
|
'Bucket' => $bucket,
|
|
'ChecksumAlgorithm' => 'SHA256',
|
|
'ChecksumSHA256' => $hash,
|
|
'Key' => $prefix.$filename,
|
|
'Body' => $body,
|
|
'ACL' => 'public-read',
|
|
'ContentType' => $mimeType // pour rendre l'image publique si besoin
|
|
]);
|
|
|
|
return $doc['@metadata']['statusCode'];
|
|
}
|
|
|
|
/**
|
|
* DELETE file Object in bucket S3
|
|
*
|
|
* @param string $bucket nom du conteneur S3
|
|
* @param string $filename nom du fichier
|
|
* @param string|null $prefix arborescence dans le bucket
|
|
* @return bool
|
|
*/
|
|
public function DeleteDocObj(string $bucket, string $filename, string|null $prefix = null): int{
|
|
|
|
$doc = $this->s3Client->deleteObject([
|
|
'Bucket' => $bucket,
|
|
'Key' => $prefix.$filename,
|
|
]);
|
|
|
|
return $doc['@metadata']['statusCode'];
|
|
}
|
|
|
|
/**
|
|
* RENAME file Object in bucket S3
|
|
*
|
|
* @param string $bucket nom du conteneur S3
|
|
* @param string $filename nom du fichier
|
|
* @param string $newFilename
|
|
* @param string|null $prefix arborescence dans le bucket
|
|
* @return bool
|
|
*/
|
|
public function renameDocObj(string $bucket, string $filename, string $newFilename, string|null $prefix = null): int{
|
|
|
|
$doc = $this->s3Client->copyObject([
|
|
'Bucket' => $bucket,
|
|
'CopySource' => $prefix.$filename,
|
|
'Key' => $prefix.$newFilename,
|
|
]);
|
|
|
|
$this->DeleteDocObj($bucket, $filename, $prefix);
|
|
|
|
return $doc['@metadata']['statusCode'];
|
|
}
|
|
|
|
/**
|
|
* MOVE file Object in bucket S3
|
|
*
|
|
* @param string $bucket nom du conteneur S3
|
|
* @param string $filename nom du fichier
|
|
* @param string|null $prefix arborescence dans le bucket
|
|
* @param string|null $newPrefix nouvel emplacement dans le bucket
|
|
* @return bool
|
|
*/
|
|
public function moveDocObj(string $bucket, string $filename, string|null $prefix = null, string|null $newPrefix = null): int{
|
|
|
|
$doc = $this->s3Client->copyObject([
|
|
'Bucket' => $bucket,
|
|
'CopySource' => $prefix.$filename,
|
|
'Key' => $newPrefix.$filename,
|
|
]);
|
|
|
|
$this->DeleteDocObj($bucket, $filename, $prefix);
|
|
|
|
return $doc['@metadata']['statusCode'];
|
|
}
|
|
}
|