-
Notifications
You must be signed in to change notification settings - Fork 17
/
SwagMediaS3.php
71 lines (61 loc) · 1.72 KB
/
SwagMediaS3.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
/*
* (c) shopware AG <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
namespace SwagMediaS3;
if (file_exists(__DIR__ . '/vendor/autoload.php')) {
require_once __DIR__ . '/vendor/autoload.php';
}
use Shopware\Components\Plugin;
use League\Flysystem\AdapterInterface;
use Aws\S3\S3Client;
use League\Flysystem\AwsS3v3\AwsS3Adapter;
class SwagMediaS3 extends Plugin
{
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
'Shopware_Collect_MediaAdapter_s3' => 'createS3Adapter'
];
}
/**
* Creates adapter instance
*
* @param \Enlight_Event_EventArgs $args
* @return AdapterInterface
*/
public function createS3Adapter(\Enlight_Event_EventArgs $args)
{
$defaultConfig = [
'key' => '',
'secret' => '',
'region' => '',
'version' => 'latest',
'bucket' => '',
'prefix' => '',
'endpoint' => null,
'metaOptions' => []
];
$config = array_merge($defaultConfig, $args->get('config'));
$clientConfig = [
'region' => $config['region'],
'endpoint' => $config['endpoint'],
'version' => $config['version'],
];
if (!empty($config['key'])) {
$clientConfig['credentials'] = [
'key' => $config['key'],
'secret' => $config['secret'],
];
}
$client = new S3Client($clientConfig);
return new AwsS3Adapter($client, $config['bucket'], $config['prefix'], $config['metaOptions']);
}
}