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 DownloadItem extends CommandHandler |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Downaload 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 bool |
28
|
|
|
* @throws \Exception |
29
|
|
|
*/ |
30
|
|
|
public function handle($params = []) |
31
|
|
|
{ |
32
|
|
|
$bucketName = $params['bucket']; |
33
|
|
|
$keyName = $params['key']; |
34
|
|
|
|
35
|
|
|
if ($this->client->hasEncoder()) { |
36
|
|
|
$keyName = $this->client->getEncoder()->encode($keyName); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
try { |
40
|
|
|
$download = $this->client->getConn()->getObject([ |
41
|
|
|
'Bucket' => $bucketName, |
42
|
|
|
'Key' => $keyName, |
43
|
|
|
'SaveAs' => (isset($params['save_as'])) ? $params['save_as'] : $params['key'], |
44
|
|
|
]); |
45
|
|
|
|
46
|
|
|
if (($download instanceof ResultInterface) and $download['@metadata']['statusCode'] === 200) { |
47
|
|
|
if (null !== $this->commandHandlerLogger) { |
48
|
|
|
$this->commandHandlerLogger->log($this, sprintf('\'%s\' was successfully downloaded from bucket \'%s\'', $keyName, $bucketName)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return true; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (null !== $this->commandHandlerLogger) { |
55
|
|
|
$this->commandHandlerLogger->log($this, sprintf('Something went wrong during downloading \'%s\' from bucket \'%s\'', $keyName, $bucketName), 'warning'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return false; |
59
|
|
|
} catch (S3Exception $e) { |
60
|
|
|
if (null !== $this->commandHandlerLogger) { |
61
|
|
|
$this->commandHandlerLogger->logExceptionAndReturnFalse($e); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
throw $e; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param array $params |
70
|
|
|
* |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
|
|
public function validateParams($params = []) |
74
|
|
|
{ |
75
|
|
|
return ( |
76
|
|
|
isset($params['bucket']) and |
77
|
|
|
isset($params['key']) |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|