You've already forked symfony-storage
from wtf to package
This commit is contained in:
135
StorageBundle/Adaptors/S3Storage.php
Normal file
135
StorageBundle/Adaptors/S3Storage.php
Normal file
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
namespace Bluesquare\StorageBundle\Adaptors;
|
||||
|
||||
use Aws\S3\S3Client;
|
||||
|
||||
/**
|
||||
* Interface de manipulation du stockage S3
|
||||
* Usage: $storage = new S3Storage('my_storage_name', $config);
|
||||
* @author Maxime Renou
|
||||
*/
|
||||
class S3Storage
|
||||
{
|
||||
const MODE_PRIVATE = 'private';
|
||||
const MODE_PUBLIC = 'public-read';
|
||||
|
||||
protected $client;
|
||||
protected $bucket;
|
||||
protected $region;
|
||||
|
||||
public function __construct ($storage_name, $config)
|
||||
{
|
||||
// TODO: on check la présence de "bucket", "region", "endpoint", "credentials" ("key", "secret") dans $config
|
||||
|
||||
$this->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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
22
StorageBundle/DependencyInjection/Configuration.php
Normal file
22
StorageBundle/DependencyInjection/Configuration.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Bluesquare\StorageBundle\DependencyInjection;
|
||||
|
||||
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
|
||||
use Symfony\Component\Config\Definition\ConfigurationInterface;
|
||||
|
||||
class Configuration implements ConfigurationInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* Generates the configuration tree builder.
|
||||
*
|
||||
* @return \Symfony\Component\Config\Definition\Builder\TreeBuilder The tree builder
|
||||
*/
|
||||
public function getConfigTreeBuilder()
|
||||
{
|
||||
$treeBuilder = new TreeBuilder();
|
||||
$treeBuilder->root('storage');
|
||||
return ($treeBuilder);
|
||||
}
|
||||
}
|
||||
33
StorageBundle/DependencyInjection/StorageExtension.php
Normal file
33
StorageBundle/DependencyInjection/StorageExtension.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace Bluesquare\CryptorBundle\DependencyInjection;
|
||||
|
||||
use Symfony\Component\Config\FileLocator;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Extension\Extension;
|
||||
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
|
||||
|
||||
class StorageExtension extends Extension
|
||||
{
|
||||
|
||||
/**
|
||||
* Loads a specific configuration.
|
||||
*
|
||||
* @throws \InvalidArgumentException When provided tag is not defined in this extension
|
||||
*/
|
||||
public function load(array $configs, ContainerBuilder $container)
|
||||
{
|
||||
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Ressources/config'));
|
||||
$loader->load('services.yaml');
|
||||
$configuration = $this->getConfiguration($configs, $container);
|
||||
dump($configuration); die;
|
||||
$config = $this->processConfiguration($configuration, $configs);
|
||||
|
||||
return $config;
|
||||
}
|
||||
|
||||
public function getAlias()
|
||||
{
|
||||
return parent::getAlias(); // TODO: Change the autogenerated stub
|
||||
}
|
||||
}
|
||||
6
StorageBundle/Ressources/config/services.yaml
Normal file
6
StorageBundle/Ressources/config/services.yaml
Normal file
@@ -0,0 +1,6 @@
|
||||
services:
|
||||
bluesquare.storage:
|
||||
class: Bluesquare\StorageBundle\Storage
|
||||
autowire: true
|
||||
public: true
|
||||
Bluesquare\CryptorBundle\Cryptor: '@bluesquare.storage'
|
||||
47
StorageBundle/Storage.php
Normal file
47
StorageBundle/Storage.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace Bluesquare\StorageBundle\Adaptors;
|
||||
|
||||
use Aws\S3\S3Client;
|
||||
|
||||
/**
|
||||
* Interface de manipulation des stockages préconfigurés
|
||||
* Usage par injection
|
||||
*/
|
||||
class Storage
|
||||
{
|
||||
public function get($storage_name)
|
||||
{
|
||||
// TODO: on récupère les infos sur ce storage dans la config utilisateur (config/bluesquare/storage.yaml)
|
||||
// La clef pour la configuration d'un storage devrait être : "storage.{storage_name}"
|
||||
|
||||
// Si storage.{storage_name}.type == 's3' alors :
|
||||
return new S3Storage($storage_name, $config); // $config c'est le contenu de storage.{storage_name} sous forme de tableau
|
||||
|
||||
// Sinon :
|
||||
return null; // (on ajoutera d'autres types de stockage plus tard, dont le stockage de fichier sur le serveur actuel)
|
||||
}
|
||||
}
|
||||
|
||||
//storage:
|
||||
//photos:
|
||||
//type: s3
|
||||
// bucket: bluesquare.public
|
||||
// region: nl-ams
|
||||
// endpoint: 'https://s3.nl-ams.scw.cloud'
|
||||
// credentials:
|
||||
// key: '%env(MYSUPERSECRETAWSKEY)%'
|
||||
// secret: '%env(MYSUPERSECRETAWSSECRET)%'
|
||||
// version: lastest # optionnel
|
||||
// path: '/photos' # optionnel
|
||||
//
|
||||
// files:
|
||||
// type: s3
|
||||
// bucket: bluesquare.private
|
||||
// region: nl-ams
|
||||
// endpoint: 'https://s3.nl-ams.scw.cloud'
|
||||
// credentials:
|
||||
// key: '%env(MYSUPERSECRETAWSKEY)%'
|
||||
// secret: '%env(MYSUPERSECRETAWSSECRET)%'
|
||||
//
|
||||
// ...
|
||||
22
StorageBundle/StorageBundle.php
Normal file
22
StorageBundle/StorageBundle.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Bluesquare\StorageBundle;
|
||||
|
||||
use Bluesquare\CryptorBundle\DependencyInjection\StorageExtension;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\HttpKernel\Bundle\Bundle;
|
||||
|
||||
class StorageBundle extends Bundle
|
||||
{
|
||||
public function build(ContainerBuilder $container)
|
||||
{
|
||||
parent::build($container);
|
||||
}
|
||||
|
||||
public function getContainerExtension()
|
||||
{
|
||||
if (null === $this->extension)
|
||||
$this->extension = new StorageExtension();
|
||||
return $this->extension;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user