|
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 Exception; |
|
16
|
|
|
use Matecat\SimpleS3\Commands\CommandHandler; |
|
17
|
|
|
|
|
18
|
|
|
class SetBucketLifecycleConfiguration extends CommandHandler |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Set bucket lifecycle configuration. |
|
22
|
|
|
* For a complete reference of bucket lifecycle rules: |
|
23
|
|
|
* https://docs.aws.amazon.com/cli/latest/reference/s3api/put-bucket-lifecycle-configuration.html |
|
24
|
|
|
* |
|
25
|
|
|
* @param array $params |
|
26
|
|
|
* |
|
27
|
|
|
* @return bool |
|
28
|
|
|
* @throws Exception |
|
29
|
|
|
*/ |
|
30
|
1 |
|
public function handle(array $params = []): bool |
|
31
|
|
|
{ |
|
32
|
1 |
|
$bucketName = $params[ 'bucket' ]; |
|
33
|
1 |
|
$rules = $params[ 'rules' ]; |
|
34
|
|
|
|
|
35
|
|
|
try { |
|
36
|
1 |
|
$settings = [ |
|
37
|
1 |
|
'Bucket' => $bucketName, |
|
38
|
1 |
|
'LifecycleConfiguration' => [ |
|
39
|
1 |
|
'Rules' => $rules |
|
40
|
1 |
|
] |
|
41
|
1 |
|
]; |
|
42
|
|
|
|
|
43
|
1 |
|
$config = $this->client->getConn()->putBucketLifecycleConfiguration($settings); |
|
44
|
|
|
|
|
45
|
1 |
|
if (($config instanceof ResultInterface) and $config[ '@metadata' ][ 'statusCode' ] === 200) { |
|
46
|
1 |
|
if (null !== $this->commandHandlerLogger) { |
|
47
|
1 |
|
$this->commandHandlerLogger->log($this, sprintf('Lifecycle was successfully set for bucket \'%s\'', $bucketName)); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
return true; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$this->commandHandlerLogger?->log($this, sprintf('Something went wrong during setting of lifecycle of \'%s\' bucket', $bucketName), 'warning'); |
|
54
|
|
|
|
|
55
|
|
|
return false; |
|
56
|
|
|
} catch (Exception $e) { |
|
57
|
|
|
$this->commandHandlerLogger?->logExceptionAndReturnFalse($e); |
|
58
|
|
|
|
|
59
|
|
|
throw $e; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param array $params |
|
65
|
|
|
* |
|
66
|
|
|
* @return bool |
|
67
|
|
|
*/ |
|
68
|
1 |
|
public function validateParams(array $params = []): bool |
|
69
|
|
|
{ |
|
70
|
1 |
|
return ( |
|
71
|
1 |
|
isset($params[ 'bucket' ]) and |
|
72
|
1 |
|
isset($params[ 'rules' ]) |
|
73
|
1 |
|
); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|