1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Simple S3 package. |
4
|
|
|
* |
5
|
|
|
* (c) Mauro Cassani<https://github.com/mauretto78> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Matecat\SimpleS3\Commands\Handlers; |
13
|
|
|
|
14
|
|
|
use Aws\ResultInterface; |
15
|
|
|
use Aws\S3\Exception\S3Exception; |
16
|
|
|
use Matecat\SimpleS3\Commands\CommandHandler; |
17
|
|
|
|
18
|
|
|
class GetItem extends CommandHandler |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Get the details of an item. |
22
|
|
|
* For a complete reference: |
23
|
|
|
* https://docs.aws.amazon.com/cli/latest/reference/s3api/get-object.html |
24
|
|
|
* |
25
|
|
|
* @param array $params |
26
|
|
|
* |
27
|
|
|
* @return ResultInterface|mixed |
28
|
|
|
* @throws \Exception |
29
|
|
|
*/ |
30
|
|
|
public function handle($params = []) |
31
|
|
|
{ |
32
|
|
|
$bucketName = $params['bucket']; |
33
|
|
|
$keyName = $params['key']; |
34
|
|
|
$version = (isset($params['version'])) ? $params['version'] : null; |
35
|
|
|
|
36
|
|
|
if ($this->client->hasEncoder()) { |
37
|
|
|
$keyName = $this->client->getEncoder()->encode($keyName); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if ($this->client->hasCache() and $this->client->getCache()->has($bucketName, $keyName, $version)) { |
41
|
|
|
return $this->returnItemFromCache($bucketName, $keyName, $version); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $this->returnItemFromS3($bucketName, $keyName, $version); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param array $params |
49
|
|
|
* |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
|
|
public function validateParams($params = []) |
53
|
|
|
{ |
54
|
|
|
return ( |
55
|
|
|
isset($params['bucket']) and |
56
|
|
|
isset($params['key']) |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $bucketName |
62
|
|
|
* @param string $keyName |
63
|
|
|
* @param null $version |
|
|
|
|
64
|
|
|
* |
65
|
|
|
* @return mixed |
66
|
|
|
*/ |
67
|
|
|
private function returnItemFromCache($bucketName, $keyName, $version = null) |
68
|
|
|
{ |
69
|
|
|
if ('' === $this->client->getCache()->get($bucketName, $keyName, $version)) { |
70
|
|
|
$config = [ |
71
|
|
|
'Bucket' => $bucketName, |
72
|
|
|
'Key' => $keyName |
73
|
|
|
]; |
74
|
|
|
|
75
|
|
|
if (null != $version) { |
|
|
|
|
76
|
|
|
$config['VersionId'] = $version; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$file = $this->client->getConn()->getObject($config); |
80
|
|
|
$this->client->getCache()->set($bucketName, $keyName, $file->toArray(), $version); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this->client->getCache()->get($bucketName, $keyName, $version); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $bucketName |
88
|
|
|
* @param string $keyName |
89
|
|
|
* @param null $version |
|
|
|
|
90
|
|
|
* |
91
|
|
|
* @return array |
92
|
|
|
* @throws \Exception |
93
|
|
|
*/ |
94
|
|
|
private function returnItemFromS3($bucketName, $keyName, $version = null) |
95
|
|
|
{ |
96
|
|
|
try { |
97
|
|
|
$config = [ |
98
|
|
|
'Bucket' => $bucketName, |
99
|
|
|
'Key' => $keyName |
100
|
|
|
]; |
101
|
|
|
|
102
|
|
|
if (null != $version) { |
|
|
|
|
103
|
|
|
$config['VersionId'] = $version; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$file = $this->client->getConn()->getObject($config); |
107
|
|
|
|
108
|
|
|
if ($this->client->hasCache()) { |
109
|
|
|
$this->client->getCache()->set($bucketName, $keyName, $file->toArray(), $version); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if (null !== $this->commandHandlerLogger) { |
113
|
|
|
$this->commandHandlerLogger->log($this, sprintf('File \'%s\' was successfully obtained from \'%s\' bucket', $keyName, $bucketName)); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $file->toArray(); |
117
|
|
|
} catch (S3Exception $e) { |
118
|
|
|
if (null !== $this->commandHandlerLogger) { |
119
|
|
|
$this->commandHandlerLogger->logExceptionAndReturnFalse($e); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
throw $e; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|