commit 7a7fa86f7965614a280386043362870a0a2b2ac4 Author: Maxime Renou Date: Wed Feb 20 11:11:38 2019 +0100 Base de travail diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5408cab --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea/ +vendor/ +.DS_Store diff --git a/Adaptors/S3Storage.php b/Adaptors/S3Storage.php new file mode 100644 index 0000000..9f48b61 --- /dev/null +++ b/Adaptors/S3Storage.php @@ -0,0 +1,135 @@ +config = $config; + $this->bucket = $config['bucket']; + + $this->client = new S3Client([ + 'version' => isset($config['version']) ? $config['version'] : 'latest', + 'region' => $config['region'], + 'endpoint' => $config['endpoint'], + 'credentials' => [ + 'key' => $config['credentials']['key'], + 'secret' => $config['credentials']['secret'] + ] + ]); + } + + protected function getPrefix($prefix = null) + { + $ret = ''; + if (isset($this->config['path'])) { + $ret = trim($this->config['path'], '/').'/'; + } + if (!is_null($prefix)) { + $ret = ltrim($prefix, '/'); + } + return $ret; + } + + public function index($prefix = null) + { + return ($this->client->listObjectsV2([ + 'Bucket' => $this->bucket, + 'Prefix' => $this->getPrefix($prefix) + ])); + } + + /** + * Permet d'obtenir l'URL vers une ressource S3 + * Cette ressource n'est accessible que si le fichier a été stocké en mode public + * @param $target_path + * @return string + */ + public function url ($target_path) + { + return rtrim($this->config['endpoint'], '/').'/'.$this->getPrefix().ltrim($target_path, '/'); + } + + /** + * Permet de stocker un fichier dans S3 + * @param $source_path + * @param $target_path + * @param string $permissions + * @return \Aws\Result + */ + public function store ($source_path, $target_path, $permissions = self::MODE_PRIVATE) + { + return $this->client->putObject([ + 'Bucket' => $this->bucket, + 'Path' => $this->getPrefix().$target_path, + 'Key' => $this->getPrefix().$target_path, + 'Body' => file_get_contents($source_path), + 'ACL' => $permissions + ]); + } + + /** + * Permet de récupérer un fichier dans S3 pour le stocker en local + * @param $distant_path + * @param $local_path + */ + public function retrieve ($distant_path, $local_path) + { + $file_stream = fopen($local_path, 'w'); + + $aws_stream = $this->client->getObject([ + 'Bucket' => $this->bucket, + 'Key' => $this->getPrefix().$distant_path, + 'Path' => $this->getPrefix().$distant_path, + ])->get('Body')->detach(); + + stream_copy_to_stream($aws_stream, $file_stream); + fclose($file_stream); + } + + /** + * Permet de stream un fichier stocké dans S3 + * @param $distant_path + * @param $target_stream + */ + public function stream ($distant_path, $target_stream) + { + $aws_stream = $this->client->getObject([ + 'Bucket' => $this->bucket, + 'Key' => $this->getPrefix().$distant_path, + 'Path' => $this->getPrefix().$distant_path, + ])->get('Body')->detach(); + + stream_copy_to_stream($aws_stream, $target_stream); + } + + /** + * Permet de supprimer un fichier stocké dans S3 + * @param $distant_path + */ + public function delete ($distant_path) + { + $this->client->deleteObject([ + 'Bucket' => $this->bucket, + 'Path' => $this->getPrefix().$distant_path, + 'Key' => $this->getPrefix().$distant_path, + ]); + } +} diff --git a/Storage.php b/Storage.php new file mode 100644 index 0000000..423d109 --- /dev/null +++ b/Storage.php @@ -0,0 +1,24 @@ +