|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bex\Behat\ScreenshotExtension\Driver; |
|
4
|
|
|
|
|
5
|
|
|
use Aws\S3\S3Client; |
|
6
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
|
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
8
|
|
|
|
|
9
|
|
|
class AwsS3 implements ImageDriverInterface |
|
10
|
|
|
{ |
|
11
|
|
|
const CONFIG_PARAM_BUCKET = 'bucket'; |
|
12
|
|
|
const CONFIG_PARAM_VERSION = 'version'; |
|
13
|
|
|
const CONFIG_PARAM_REGION = 'region'; |
|
14
|
|
|
const CONFIG_PARAM_CREDENTIALS_KEY = 'credentials_key'; |
|
15
|
|
|
const CONFIG_PARAM_CREDENTIALS_SECRET = 'credentials_secret'; |
|
16
|
|
|
const CONFIG_PARAM_CREDENTIALS_TOKEN = 'credentials_token'; |
|
17
|
|
|
const CONFIG_PARAM_CLIENT_FACTORY = 'client_factory'; |
|
18
|
|
|
const CONFIG_PARAM_NAMESPACE = 'namespace'; |
|
19
|
|
|
const CONFIG_PARAM_TIMEOUT = 'timeout'; |
|
20
|
|
|
const CONFIG_PARAM_VISIBILITY = 'visibility'; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var S3Client |
|
23
|
|
|
*/ |
|
24
|
|
|
private $api; |
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
private $bucket; |
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
private $namespace; |
|
33
|
|
|
/** |
|
34
|
|
|
* @var int |
|
35
|
|
|
*/ |
|
36
|
|
|
private $timeout; |
|
37
|
|
|
/** |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
private $visibility; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param ArrayNodeDefinition $builder |
|
44
|
|
|
*/ |
|
45
|
|
|
public function configure(ArrayNodeDefinition $builder) |
|
46
|
|
|
{ |
|
47
|
|
|
$builder |
|
48
|
|
|
->children() |
|
49
|
|
|
->scalarNode(self::CONFIG_PARAM_BUCKET)->isRequired()->end() |
|
50
|
|
|
->scalarNode(self::CONFIG_PARAM_VERSION)->defaultValue('latest')->end() |
|
51
|
|
|
->scalarNode(self::CONFIG_PARAM_REGION)->isRequired()->end() |
|
52
|
|
|
->scalarNode(self::CONFIG_PARAM_CREDENTIALS_KEY)->defaultNull()->end() |
|
53
|
|
|
->scalarNode(self::CONFIG_PARAM_CREDENTIALS_SECRET)->defaultNull()->end() |
|
54
|
|
|
->scalarNode(self::CONFIG_PARAM_CREDENTIALS_TOKEN)->defaultNull()->end() |
|
55
|
|
|
->scalarNode(self::CONFIG_PARAM_CLIENT_FACTORY)->defaultNull()->end() |
|
56
|
|
|
->scalarNode(self::CONFIG_PARAM_NAMESPACE)->defaultNull()->end() |
|
57
|
|
|
->integerNode(self::CONFIG_PARAM_TIMEOUT)->defaultValue(30)->end() |
|
58
|
|
|
->enumNode(self::CONFIG_PARAM_VISIBILITY) |
|
59
|
|
|
->values(['public-read', 'private']) |
|
60
|
|
|
->defaultValue('public-read') |
|
61
|
|
|
->end() |
|
62
|
|
|
->end(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param ContainerBuilder $container |
|
67
|
|
|
* @param array $config |
|
68
|
|
|
*/ |
|
69
|
|
|
public function load(ContainerBuilder $container, array $config) |
|
70
|
|
|
{ |
|
71
|
|
|
$this->timeout = $config[self::CONFIG_PARAM_TIMEOUT]; |
|
72
|
|
|
$this->bucket = $config[self::CONFIG_PARAM_BUCKET]; |
|
73
|
|
|
$this->visibility = $config[self::CONFIG_PARAM_VISIBILITY]; |
|
74
|
|
|
|
|
75
|
|
|
$this->namespace = $config[self::CONFIG_PARAM_NAMESPACE] |
|
76
|
|
|
?: sprintf('%s-%04d', date('Y-m-d_H-i-s'), rand(0, 9999)); |
|
77
|
|
|
$version = $config[self::CONFIG_PARAM_VERSION]; |
|
78
|
|
|
$region = $config[self::CONFIG_PARAM_REGION]; |
|
79
|
|
|
$credentials = null; |
|
80
|
|
|
if ($config[self::CONFIG_PARAM_CREDENTIALS_KEY] && $config[self::CONFIG_PARAM_CREDENTIALS_SECRET]) { |
|
81
|
|
|
$credentials = [ |
|
82
|
|
|
'key' => $config[self::CONFIG_PARAM_CREDENTIALS_KEY], |
|
83
|
|
|
'secret' => $config[self::CONFIG_PARAM_CREDENTIALS_SECRET], |
|
84
|
|
|
'token' => $config[self::CONFIG_PARAM_CREDENTIALS_TOKEN], |
|
85
|
|
|
]; |
|
86
|
|
|
} elseif ($config[self::CONFIG_PARAM_CREDENTIALS_KEY] === false) { |
|
87
|
|
|
$credentials = false; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$clientFactory = $config[self::CONFIG_PARAM_CLIENT_FACTORY] ?: [$this, 'createClient']; |
|
91
|
|
|
if (!is_callable($clientFactory)) { |
|
92
|
|
|
throw new \RuntimeException('Invalid S3 API client factory callback'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$args = [ |
|
96
|
|
|
'version' => $version, |
|
97
|
|
|
'region' => $region, |
|
98
|
|
|
'credentials' => $credentials, |
|
99
|
|
|
]; |
|
100
|
|
|
|
|
101
|
|
|
$this->api = call_user_func($clientFactory, $args); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param string $binaryImage |
|
106
|
|
|
* @param string $filename |
|
107
|
|
|
* |
|
108
|
|
|
* @return string URL to the image |
|
109
|
|
|
*/ |
|
110
|
|
|
public function upload($binaryImage, $filename) |
|
111
|
|
|
{ |
|
112
|
|
|
$path = join('/', [$this->namespace, $filename]); |
|
113
|
|
|
$options = ['params' => ['ContentType' => 'image/png']]; |
|
114
|
|
|
|
|
115
|
|
|
$result = $this->api->upload($this->bucket, $path, $binaryImage, $this->visibility, $options); |
|
116
|
|
|
|
|
117
|
|
|
if ('public-read' === $this->visibility) { |
|
118
|
|
|
return $result['ObjectURL']; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$command = $this->api->getCommand('GetObject', [ |
|
122
|
|
|
'Bucket' => $this->bucket, |
|
123
|
|
|
'Key' => $path, |
|
124
|
|
|
]); |
|
125
|
|
|
|
|
126
|
|
|
return $this->api |
|
127
|
|
|
->createPresignedRequest( |
|
128
|
|
|
$command, |
|
129
|
|
|
sprintf('+%d minutes', $this->timeout) |
|
130
|
|
|
) |
|
131
|
|
|
->getUri(); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param array $args |
|
136
|
|
|
* |
|
137
|
|
|
* @return S3Client |
|
138
|
|
|
*/ |
|
139
|
|
|
public function createClient($args) |
|
140
|
|
|
{ |
|
141
|
|
|
return new S3Client($args); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|