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
|
|
|
public function handle($params = []) |
31
|
|
|
{ |
32
|
|
|
$bucketName = $params['bucket']; |
33
|
|
|
$rules = $params['rules']; |
34
|
|
|
|
35
|
|
|
try { |
36
|
|
|
$settings = [ |
37
|
|
|
'Bucket' => $bucketName, |
38
|
|
|
'LifecycleConfiguration' => [ |
39
|
|
|
'Rules' => $rules |
40
|
|
|
] |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
$config = $this->client->getConn()->putBucketLifecycleConfiguration($settings); |
44
|
|
|
|
45
|
|
|
if (($config instanceof ResultInterface) and $config['@metadata']['statusCode'] === 200) { |
46
|
|
|
if (null !== $this->commandHandlerLogger) { |
47
|
|
|
$this->commandHandlerLogger->log($this, sprintf('Lifecycle was successfully set for bucket \'%s\'', $bucketName)); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return true; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (null !== $this->commandHandlerLogger) { |
54
|
|
|
$this->commandHandlerLogger->log($this, sprintf('Something went wrong during setting of lifecycle of \'%s\' bucket', $bucketName), 'warning'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return false; |
58
|
|
|
} catch (\Exception $e) { |
59
|
|
|
if (null !== $this->commandHandlerLogger) { |
60
|
|
|
$this->commandHandlerLogger->logExceptionAndReturnFalse($e); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
throw $e; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param array $params |
69
|
|
|
* |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
|
|
public function validateParams($params = []) |
73
|
|
|
{ |
74
|
|
|
return ( |
75
|
|
|
isset($params['bucket']) and |
76
|
|
|
isset($params['rules']) |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|