Skip to content

Commit 93fb450

Browse files
authored
feat(storage): bucket lifecycle management (GoogleCloudPlatform#1128)
1 parent 752b14b commit 93fb450

File tree

5 files changed

+215
-0
lines changed

5 files changed

+215
-0
lines changed

storage/composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121
"src/delete_object.php",
2222
"src/delete_object_acl.php",
2323
"src/delete_hmac_key.php",
24+
"src/disable_bucket_lifecycle_management.php",
2425
"src/disable_uniform_bucket_level_access.php",
2526
"src/disable_default_event_based_hold.php",
2627
"src/disable_requester_pays.php",
2728
"src/deactivate_hmac_key.php",
2829
"src/download_encrypted_object.php",
2930
"src/download_file_requester_pays.php",
3031
"src/download_object.php",
32+
"src/enable_bucket_lifecycle_management.php",
3133
"src/enable_uniform_bucket_level_access.php",
3234
"src/enable_default_event_based_hold.php",
3335
"src/enable_default_kms_key.php",
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
/**
3+
* Copyright 2020 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/storage/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START storage_disable_bucket_lifecycle_management]
27+
use Google\Cloud\Storage\StorageClient;
28+
use Google\Cloud\Storage\Bucket;
29+
30+
/**
31+
* Disable bucket lifecycle management.
32+
*
33+
* @param string $bucketName the name of your Cloud Storage bucket.
34+
*/
35+
function disable_bucket_lifecycle_management($bucketName)
36+
{
37+
$storage = new StorageClient();
38+
$bucket = $storage->bucket($bucketName);
39+
40+
$bucket->update([
41+
'lifecycle' => null
42+
]);
43+
44+
printf('Lifecycle management is disabled for bucket %s.' . PHP_EOL, $bucketName);
45+
}
46+
# [END storage_disable_bucket_lifecycle_management]
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
/**
3+
* Copyright 2020 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/storage/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START storage_enable_bucket_lifecycle_management]
27+
use Google\Cloud\Storage\StorageClient;
28+
use Google\Cloud\Storage\Bucket;
29+
30+
/**
31+
* Enable bucket lifecycle management.
32+
*
33+
* @param string $bucketName the name of your Cloud Storage bucket.
34+
*/
35+
function enable_bucket_lifecycle_management($bucketName)
36+
{
37+
$storage = new StorageClient();
38+
$bucket = $storage->bucket($bucketName);
39+
40+
$lifecycle = Bucket::lifecycle()
41+
->addDeleteRule([
42+
'age' => 100
43+
]);
44+
45+
$bucket->update([
46+
'lifecycle' => $lifecycle
47+
]);
48+
49+
$lifecycle = $bucket->currentLifecycle();
50+
51+
printf('Lifecycle management is enabled for bucket %s and the rules are:' . PHP_EOL, $bucketName);
52+
foreach ($lifecycle as $rule) {
53+
print_r($rule);
54+
print(PHP_EOL);
55+
}
56+
}
57+
# [END storage_enable_bucket_lifecycle_management]

storage/storage.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,29 @@
635635
generate_v4_post_policy($bucketName, $objectName);
636636
});
637637

638+
$application->add(new Command('bucket-lifecycle-management'))
639+
->setDescription('Manages lifecycle rules for a bucket.')
640+
->setHelp(<<
641+
The %command.name% command enables or disables lifecycles rules for a bucket.
642+
643+
php %command.full_name% --help
644+
645+
EOF
646+
)
647+
->addArgument('bucket', InputArgument::REQUIRED, 'The Cloud Storage bucket name')
648+
->addOption('enable', null, InputOption::VALUE_NONE, 'Enable lifecycle management on a Cloud Storage bucket')
649+
->addOption('disable', null, InputOption::VALUE_NONE, 'Disable lifecycle management on a Cloud Storage bucket')
650+
->setCode(function ($input, $output) {
651+
$bucketName = $input->getArgument('bucket');
652+
if ($input->getOption('enable')) {
653+
enable_bucket_lifecycle_management($bucketName);
654+
} elseif ($input->getOption('disable')) {
655+
disable_bucket_lifecycle_management($bucketName);
656+
} else {
657+
throw new \Exception('You must provide --enable or --disable with a bucket name.');
658+
}
659+
});
660+
638661
// for testing
639662
if (getenv('PHPUNIT_TESTS') === '1') {
640663
return $application;
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
2+
/**
3+
* Copyright 2020 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Google\Cloud\Samples\Storage\Tests;
19+
20+
use Google\Cloud\Storage\StorageClient;
21+
use Google\Cloud\TestUtils\TestTrait;
22+
use Google\Cloud\TestUtils\ExecuteCommandTrait;
23+
use PHPUnit\Framework\TestCase;
24+
25+
/**
26+
* Unit Tests for enable/disable bucket lifecycle management.
27+
*/
28+
class BucketLifecycleManagementTest extends TestCase
29+
{
30+
use TestTrait;
31+
use ExecuteCommandTrait;
32+
33+
private static $commandFile = __DIR__ . '/../storage.php';
34+
protected $bucket;
35+
36+
public function setUp()
37+
{
38+
$storage = new StorageClient();
39+
$bucketName = sprintf('php-olm-%s-%s', time(), rand(1000, 9999));
40+
$this->useResourceExhaustedBackoff();
41+
self::$backoff->execute(function () use ($storage, $bucketName) {
42+
$this->bucket = $storage->createBucket($bucketName);
43+
});
44+
}
45+
46+
public function tearDown()
47+
{
48+
$this->bucket->delete();
49+
}
50+
51+
public function testEnableBucketLifecycleManagement()
52+
{
53+
$bucketName = $this->bucket->name();
54+
$output = $this->runCommand('bucket-lifecycle-management', [
55+
'bucket' => $bucketName,
56+
'--enable' => true,
57+
]);
58+
$match = "Lifecycle management is enabled for bucket $bucketName and the rules are:";
59+
$this->assertContains($match, $output);
60+
$this->bucket->reload();
61+
$lifecycle = $this->bucket->currentLifecycle()->toArray();
62+
$rules = $lifecycle['rule'];
63+
$this->assertContains([
64+
'action' => [
65+
'type' => 'Delete'
66+
],
67+
'condition' => [
68+
'age' => 100
69+
]
70+
], $rules);
71+
}
72+
73+
/** @depends testEnableBucketLifecycleManagement */
74+
public function testDisableBucketLifecycleManagement()
75+
{
76+
$bucketName = $this->bucket->name();
77+
$output = $this->runCommand('bucket-lifecycle-management', [
78+
'bucket' => $bucketName,
79+
'--disable' => true,
80+
]);
81+
$expectedOutput = "Lifecycle management is disabled for bucket $bucketName.\n";
82+
$this->assertEquals($expectedOutput, $output);
83+
$this->bucket->reload();
84+
$lifecycle = $this->bucket->currentLifecycle()->toArray();
85+
$this->assertEmpty($lifecycle);
86+
}
87+
}

0 commit comments

Comments
 (0)