Skip to content

Commit

Permalink
Added S3 adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
arnegroskurth committed Jul 13, 2018
1 parent c64cafc commit cd784f9
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"symfony/filesystem": "^4.0",
"symfony/finder": "^4.0",
"symfony/validator": "^4.0",
"zendframework/zend-stdlib": "^3.1"
"zendframework/zend-stdlib": "^3.1",
"league/flysystem-aws-s3-v3": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^7.1",
Expand Down
2 changes: 2 additions & 0 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use Storeman\OperationListBuilder\StandardOperationListBuilder;
use Storeman\StorageAdapter\Ftp;
use Storeman\StorageAdapter\Local;
use Storeman\StorageAdapter\S3;
use Storeman\StorageAdapter\StorageAdapterInterface;
use Storeman\Validation\Constraints\ExistingServiceValidator;
use Storeman\VaultLayout\Amberjack\AmberjackVaultLayout;
Expand Down Expand Up @@ -124,6 +125,7 @@ public function __construct()
$this->registerVaultServiceFactory('storageAdapter', 'adapter');
$this->addStorageAdapter('ftp', Ftp::class)->withArgument('vaultConfiguration');
$this->addStorageAdapter('local', Local::class)->withArgument('vaultConfiguration');
$this->addStorageAdapter('s3', S3::class)->withArgument('vaultConfiguration');

$this->registerVaultServiceFactory('vaultLayout');
$this->addVaultLayout('amberjack', AmberjackVaultLayout::class)->withArguments(['storageAdapter']);
Expand Down
47 changes: 47 additions & 0 deletions src/StorageAdapter/S3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Storeman\StorageAdapter;

use Aws\S3\S3Client;
use League\Flysystem\AwsS3v3\AwsS3Adapter;
use League\Flysystem\Filesystem;
use Storeman\Config\ConfigurationException;
use Storeman\Config\VaultConfiguration;

class S3 extends FlysystemStorageAdapter
{
public function __construct(VaultConfiguration $vaultConfiguration)
{
$requiredSettings = [
'bucket',
'key',
'secret',
];

if ($missingSettings = array_diff($requiredSettings, array_keys(array_filter($vaultConfiguration->getSettings()))))
{
throw new ConfigurationException("Missing mandatory setting(s): " . implode(',', $missingSettings));
}

$client = new S3Client([
'version' => '2006-03-01',
'credentials' => [
'key' => $vaultConfiguration->getSetting('key'),
'secret' => $vaultConfiguration->getSetting('secret'),
],
'region' => 'eu-central-1',
]);
$adapter = new AwsS3Adapter($client, $vaultConfiguration->getSetting('bucket'));
$filesystem = new Filesystem($adapter);

parent::__construct($filesystem);
}

/**
* {@inheritdoc}
*/
public static function getIdentificationString(VaultConfiguration $vaultConfiguration): string
{
return "{$vaultConfiguration->getSetting('username')}@{$vaultConfiguration->getSetting('host')}";
}
}
25 changes: 25 additions & 0 deletions tests/StorageAdapter/S3Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Storeman\Test\StorageAdapter;

use Storeman\Config\VaultConfiguration;
use Storeman\StorageAdapter\S3;
use Storeman\StorageAdapter\StorageAdapterInterface;
use Storeman\Test\ConfiguredMockProviderTrait;

class S3Test extends AbstractStorageAdapterTest
{
use ConfiguredMockProviderTrait;

protected function getStorageAdapter(): StorageAdapterInterface
{
$vaultConfiguration = new VaultConfiguration($this->getConfigurationMock());
$vaultConfiguration->setSettings([
'key' => getenv('s3key'),
'secret' => getenv('s3secret'),
'bucket' => getenv('s3bucket'),
]);

return new S3($vaultConfiguration);
}
}

0 comments on commit cd784f9

Please sign in to comment.