forked from shopware5/SwagMediaS3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SwagMediaS3.php
65 lines (56 loc) · 1.63 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
<?php
/*
* (c) shopware AG <info@shopware.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
namespace SwagMediaS3;
require __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
{
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']);
}
}