|
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 Exception; |
|
17
|
|
|
use Matecat\SimpleS3\Commands\CommandHandler; |
|
18
|
|
|
use Matecat\SimpleS3\Helpers\File; |
|
19
|
|
|
|
|
20
|
|
|
class CreateFolder extends CommandHandler |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Create a folder. |
|
24
|
|
|
* For a complete reference: |
|
25
|
|
|
* https://docs.aws.amazon.com/cli/latest/reference/s3api/put-object.html?highlight=put |
|
26
|
|
|
* |
|
27
|
|
|
* @param array $params |
|
28
|
|
|
* |
|
29
|
|
|
* @return bool |
|
30
|
|
|
* @throws Exception |
|
31
|
|
|
*/ |
|
32
|
1 |
|
public function handle(array $params = []): bool |
|
33
|
|
|
{ |
|
34
|
1 |
|
$bucketName = $params[ 'bucket' ]; |
|
35
|
1 |
|
$keyName = $params[ 'key' ]; |
|
36
|
|
|
|
|
37
|
1 |
|
if ($this->client->hasEncoder()) { |
|
38
|
1 |
|
$keyName = $this->client->getEncoder()->encode($keyName); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
1 |
|
if (false === File::endsWith($keyName, $this->client->getPrefixSeparator())) { |
|
42
|
1 |
|
$keyName .= $this->client->getPrefixSeparator(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
try { |
|
46
|
1 |
|
$folder = $this->client->getConn()->putObject([ |
|
47
|
1 |
|
'Bucket' => $bucketName, |
|
48
|
1 |
|
'Key' => $keyName, |
|
49
|
1 |
|
'Body' => '', |
|
50
|
1 |
|
// 'ACL' => 'public-read' // only bucket owners are allowed to create folder with ACL or bucket owner enforcement must be disabled |
|
51
|
1 |
|
]); |
|
52
|
|
|
|
|
53
|
1 |
|
if (($folder instanceof ResultInterface) and $folder[ '@metadata' ][ 'statusCode' ] === 200) { |
|
54
|
1 |
|
$this->commandHandlerLogger?->log($this, sprintf('Folder \'%s\' was successfully created in \'%s\' bucket', $keyName, $bucketName)); |
|
55
|
|
|
|
|
56
|
1 |
|
return true; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$this->commandHandlerLogger?->log($this, sprintf('Something went wrong during creation of \'%s\' folder inside \'%s\' bucket', $keyName, $bucketName), 'warning'); |
|
60
|
|
|
|
|
61
|
|
|
return false; |
|
62
|
|
|
} catch (S3Exception $e) { |
|
63
|
|
|
$this->commandHandlerLogger?->logExceptionAndReturnFalse($e); |
|
64
|
|
|
|
|
65
|
|
|
throw $e; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param array $params |
|
71
|
|
|
* |
|
72
|
|
|
* @return bool |
|
73
|
|
|
*/ |
|
74
|
1 |
|
public function validateParams(array $params = []): bool |
|
75
|
|
|
{ |
|
76
|
1 |
|
return ( |
|
77
|
1 |
|
isset($params[ 'bucket' ]) and |
|
78
|
1 |
|
isset($params[ 'key' ]) |
|
79
|
1 |
|
); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|