|
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\S3\Exception\S3Exception; |
|
15
|
|
|
use Exception; |
|
16
|
|
|
use Matecat\SimpleS3\Commands\CommandHandler; |
|
17
|
|
|
use Matecat\SimpleS3\Helpers\File; |
|
18
|
|
|
|
|
19
|
|
|
class HasFolder extends CommandHandler |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Check if a folder already exists. |
|
23
|
|
|
* For a complete reference: |
|
24
|
|
|
* https://docs.aws.amazon.com/cli/latest/reference/s3api/head-object.html |
|
25
|
|
|
* |
|
26
|
|
|
* @param array $params |
|
27
|
|
|
* |
|
28
|
|
|
* @return bool |
|
29
|
|
|
* @throws Exception |
|
30
|
|
|
*/ |
|
31
|
1 |
|
public function handle(array $params = []): bool |
|
32
|
|
|
{ |
|
33
|
1 |
|
$bucketName = $params[ 'bucket' ]; |
|
34
|
1 |
|
$prefix = $params[ 'prefix' ]; |
|
35
|
|
|
|
|
36
|
1 |
|
if ($this->client->hasEncoder()) { |
|
37
|
1 |
|
$prefix = $this->client->getEncoder()->encode($prefix); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
1 |
|
if (false === File::endsWith($prefix, $this->client->getPrefixSeparator())) { |
|
41
|
1 |
|
$prefix .= $this->client->getPrefixSeparator(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
1 |
|
if ($this->client->hasCache() and $this->client->getCache()->has($bucketName, $prefix)) { |
|
45
|
|
|
return true; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
return $this->returnItemFromS3($bucketName, $prefix); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param array $params |
|
53
|
|
|
* |
|
54
|
|
|
* @return bool |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public function validateParams(array $params = []): bool |
|
57
|
|
|
{ |
|
58
|
1 |
|
return ( |
|
59
|
1 |
|
isset($params[ 'bucket' ]) and |
|
60
|
1 |
|
isset($params[ 'prefix' ]) |
|
61
|
1 |
|
); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string $bucketName |
|
66
|
|
|
* @param string $prefix |
|
67
|
|
|
* |
|
68
|
|
|
* @return bool |
|
69
|
|
|
* @throws Exception |
|
70
|
|
|
*/ |
|
71
|
1 |
|
private function returnItemFromS3(string $bucketName, string $prefix): bool |
|
72
|
|
|
{ |
|
73
|
1 |
|
$command = $this->client->getConn()->getCommand( |
|
74
|
1 |
|
'listObjects', |
|
75
|
1 |
|
[ |
|
76
|
1 |
|
'Bucket' => $bucketName, |
|
77
|
1 |
|
'Prefix' => $prefix, |
|
78
|
1 |
|
'MaxKeys' => 1, |
|
79
|
1 |
|
] |
|
80
|
1 |
|
); |
|
81
|
|
|
try { |
|
82
|
1 |
|
$result = $this->client->getConn()->execute($command); |
|
83
|
|
|
|
|
84
|
1 |
|
return $result[ 'Contents' ] || $result[ 'CommonPrefixes' ]; |
|
85
|
|
|
} catch (S3Exception $e) { |
|
86
|
|
|
$this->commandHandlerLogger?->logExceptionAndReturnFalse($e); |
|
87
|
|
|
|
|
88
|
|
|
throw $e; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|