1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BaoPham\DynamoDb; |
4
|
|
|
|
5
|
|
|
use Aws\DynamoDb\DynamoDbClient; |
6
|
|
|
use Aws\DynamoDb\Marshaler; |
7
|
|
|
use Aws\Sts\StsClient; |
8
|
|
|
use Aws\Sts\Exception\StsException; |
9
|
|
|
use Illuminate\Support\Arr; |
10
|
|
|
use Illuminate\Support\Facades\Log; |
11
|
|
|
|
12
|
|
|
class DynamoDbClientService implements DynamoDbClientInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected $clients; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var \Aws\DynamoDb\Marshaler |
21
|
|
|
*/ |
22
|
|
|
protected $marshaler; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var \BaoPham\DynamoDb\EmptyAttributeFilter |
26
|
|
|
*/ |
27
|
|
|
protected $attributeFilter; |
28
|
|
|
|
29
|
145 |
|
public function __construct(Marshaler $marshaler, EmptyAttributeFilter $filter) |
30
|
|
|
{ |
31
|
145 |
|
$this->marshaler = $marshaler; |
32
|
145 |
|
$this->attributeFilter = $filter; |
33
|
145 |
|
$this->clients = []; |
34
|
145 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return \Aws\DynamoDb\DynamoDbClient |
38
|
|
|
*/ |
39
|
132 |
|
public function getClient($connection = null) |
40
|
|
|
{ |
41
|
132 |
|
$connection = $connection ?: config('dynamodb.default'); |
42
|
|
|
|
43
|
132 |
|
if (isset($this->clients[$connection])) { |
44
|
120 |
|
return $this->clients[$connection]; |
45
|
|
|
} |
46
|
|
|
|
47
|
132 |
|
$config = config("dynamodb.connections.$connection", []); |
48
|
132 |
|
$config['version'] = '2012-08-10'; |
49
|
132 |
|
$config['debug'] = $this->getDebugOptions(Arr::get($config, 'debug')); |
50
|
|
|
|
51
|
132 |
|
if (array_key_exists('assume_role_arn', $config)) { |
52
|
|
|
try { |
53
|
|
|
$stsConfig = $config; |
54
|
|
|
$stsConfig['version'] = "2011-06-15"; |
55
|
|
|
$stsClient = new StsClient($stsConfig); |
56
|
|
|
$result = $stsClient->AssumeRole([ |
57
|
|
|
'DurationSeconds' => 900, |
58
|
|
|
'RoleArn' => $config['assume_role_arn'], |
59
|
|
|
'RoleSessionName' => config('app.name') . '-dynamodb', |
60
|
|
|
]); |
61
|
|
|
} catch (StsException $e) { |
62
|
|
|
Log::error($e->getTraceAsString()); |
63
|
|
|
return false; |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$config['credentials'] = [ |
67
|
|
|
'key' => $result['Credentials']['AccessKeyId'], |
68
|
|
|
'secret' => $result['Credentials']['SecretAccessKey'], |
69
|
|
|
'token' => $result['Credentials']['SessionToken'], |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
132 |
|
$client = new DynamoDbClient($config); |
74
|
|
|
|
75
|
132 |
|
$this->clients[$connection] = $client; |
76
|
|
|
|
77
|
132 |
|
return $client; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return \Aws\DynamoDb\Marshaler |
82
|
|
|
*/ |
83
|
138 |
|
public function getMarshaler() |
84
|
|
|
{ |
85
|
138 |
|
return $this->marshaler; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return \BaoPham\DynamoDb\EmptyAttributeFilter |
90
|
|
|
*/ |
91
|
131 |
|
public function getAttributeFilter() |
92
|
|
|
{ |
93
|
131 |
|
return $this->attributeFilter; |
94
|
|
|
} |
95
|
|
|
|
96
|
132 |
|
protected function getDebugOptions($debug = false) |
97
|
|
|
{ |
98
|
132 |
|
if ($debug === true) { |
99
|
|
|
$logfn = function ($msg) { |
100
|
|
|
Log::info($msg); |
101
|
|
|
}; |
102
|
|
|
|
103
|
|
|
return ['logfn' => $logfn]; |
104
|
|
|
} |
105
|
|
|
|
106
|
132 |
|
return $debug; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|