Skip to content

Commit cc58f22

Browse files
authored
Merge pull request GoogleCloudPlatform#692 from GoogleCloudPlatform/bucketlock
PHP Bucketlock samples and tests
2 parents 74195bb + 18f166e commit cc58f22

16 files changed

+870
-3
lines changed

storage/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ This simple command-line application demonstrates how to invoke Google Cloud Sto
3535
bucket-acl Manage the ACL for Cloud Storage buckets.
3636
bucket-default-acl Manage the default ACL for Cloud Storage buckets.
3737
bucket-labels Manage Cloud Storage bucket labels
38+
bucket-lock Manage Cloud Storage retention policies and holds
3839
buckets Manage Cloud Storage buckets
3940
encryption Upload and download Cloud Storage objects with encryption
4041
object-acl Manage the ACL for Cloud Storage objects

storage/composer.json

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"require": {
3-
"google/cloud-storage": "^1.5.2",
3+
"google/cloud-storage": "^1.9.0",
44
"paragonie/random_compat": "^2.0",
55
"symfony/console": " ^3.0"
66
},
@@ -18,13 +18,14 @@
1818
"src/delete_bucket_default_acl.php",
1919
"src/delete_object.php",
2020
"src/delete_object_acl.php",
21+
"src/disable_default_event_based_hold.php",
2122
"src/disable_requester_pays.php",
2223
"src/download_encrypted_object.php",
2324
"src/download_file_requester_pays.php",
2425
"src/download_object.php",
26+
"src/enable_default_event_based_hold.php",
2527
"src/enable_default_kms_key.php",
2628
"src/enable_requester_pays.php",
27-
"src/object_metadata.php",
2829
"src/generate_encryption_key.php",
2930
"src/get_bucket_acl.php",
3031
"src/get_bucket_acl_for_entity.php",
@@ -34,14 +35,24 @@
3435
"src/get_object_acl.php",
3536
"src/get_object_acl_for_entity.php",
3637
"src/get_requester_pays_status.php",
38+
"src/get_retention_policy.php",
39+
"src/get_default_event_based_hold.php",
3740
"src/list_buckets.php",
3841
"src/list_objects.php",
3942
"src/list_objects_with_prefix.php",
43+
"src/lock_retention_policy.php",
4044
"src/make_public.php",
4145
"src/move_object.php",
46+
"src/object_metadata.php",
47+
"src/release_event_based_hold.php",
48+
"src/release_temporary_hold.php",
4249
"src/remove_bucket_iam_member.php",
4350
"src/remove_bucket_label.php",
51+
"src/remove_retention_policy.php",
4452
"src/rotate_encryption_key.php",
53+
"src/set_event_based_hold.php",
54+
"src/set_retention_policy.php",
55+
"src/set_temporary_hold.php",
4556
"src/upload_encrypted_object.php",
4657
"src/upload_object.php",
4758
"src/upload_with_kms_key.php",
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
/**
3+
* Copyright 2018 Google Inc.
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/api/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START storage_disable_default_event_based_hold]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Disables a default event-based hold for a bucket.
31+
*
32+
* @param string $bucketName the name of your Cloud Storage bucket.
33+
*/
34+
function disable_default_event_based_hold($bucketName)
35+
{
36+
$storage = new StorageClient();
37+
$bucket = $storage->bucket($bucketName);
38+
$bucket->update(['defaultEventBasedHold' => false]);
39+
printf('Default event-based hold was disabled for %s' . PHP_EOL, $bucketName);
40+
}
41+
# [END storage_disable_default_event_based_hold]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
/**
3+
* Copyright 2018 Google Inc.
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/api/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START storage_enable_default_event_based_hold]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Enables a default event-based hold for a bucket.
31+
*
32+
* @param string $bucketName the name of your Cloud Storage bucket.
33+
*/
34+
function enable_default_event_based_hold($bucketName)
35+
{
36+
$storage = new StorageClient();
37+
$bucket = $storage->bucket($bucketName);
38+
$bucket->update(['defaultEventBasedHold' => true]);
39+
printf('Default event-based hold was enabled for %s' . PHP_EOL, $bucketName);
40+
}
41+
# [END storage_enable_default_event_based_hold]
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
/**
3+
* Copyright 2018 Google Inc.
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/api/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START storage_get_default_event_based_hold]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Enables a default event-based hold for a bucket.
31+
*
32+
* @param string $bucketName the name of your Cloud Storage bucket.
33+
*/
34+
function get_default_event_based_hold($bucketName)
35+
{
36+
$storage = new StorageClient();
37+
$bucket = $storage->bucket($bucketName);
38+
39+
if ($bucket->info()['defaultEventBasedHold']) {
40+
printf('Default event-based hold is enabled for ' . $bucketName . PHP_EOL);
41+
} else {
42+
printf('Default event-based hold is not enabled for ' . $bucketName . PHP_EOL);
43+
}
44+
45+
}
46+
# [END storage_get_default_event_based_hold]

storage/src/get_retention_policy.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
/**
3+
* Copyright 2018 Google Inc.
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/api/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START storage_get_retention_policy]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Gets a bucket's retention policy.
31+
*
32+
* @param string $bucketName the name of your Cloud Storage bucket.
33+
*/
34+
function get_retention_policy($bucketName)
35+
{
36+
$storage = new StorageClient();
37+
$bucket = $storage->bucket($bucketName);
38+
$bucket->reload();
39+
40+
printf('Retention Policy for ' . $bucketName . PHP_EOL);
41+
printf('Retention Period: ' . $bucket->info()['retentionPolicy']['retentionPeriod'] . PHP_EOL);
42+
if (array_key_exists('isLocked', $bucket->info()['retentionPolicy']) &&
43+
$bucket->info()['retentionPolicy']['isLocked']) {
44+
printf('Retention Policy is locked' . PHP_EOL);
45+
}
46+
if ($bucket->info()['retentionPolicy']['effectiveTime']) {
47+
printf('Effective Time: ' . $bucket->info()['retentionPolicy']['effectiveTime'] . PHP_EOL);
48+
}
49+
}
50+
# [END storage_get_retention_policy]

storage/src/lock_retention_policy.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
/**
3+
* Copyright 2018 Google Inc.
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/api/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START storage_lock_retention_policy]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Locks a bucket's retention policy.
31+
*
32+
* @param string $bucketName the name of your Cloud Storage bucket.
33+
*/
34+
function lock_retention_policy($bucketName)
35+
{
36+
$storage = new StorageClient();
37+
$bucket = $storage->bucket($bucketName);
38+
$bucket->reload();
39+
$bucket->lockRetentionPolicy();
40+
printf('Bucket %s retention policy locked' . PHP_EOL, $bucketName);
41+
}
42+
# [END storage_lock_retention_policy]

storage/src/object_metadata.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ function object_metadata($bucketName, $objectName)
5252
printf('Crc32c: %s' . PHP_EOL, $info['crc32c']);
5353
printf('MD5 Hash: %s' . PHP_EOL, $info['md5Hash']);
5454
printf('Content-type: %s' . PHP_EOL, $info['contentType']);
55+
printf("Temporary hold: " . ($info['temporaryHold'] ? "enabled" : "disabled") . PHP_EOL);
56+
printf("Event-based hold: " . ($info['eventBasedHold'] ? "enabled" : "disabled") . PHP_EOL);
57+
if ($info['retentionExpirationTime']) {
58+
printf("retentionExpirationTime: " . $info['retentionExpirationTime'] . PHP_EOL);
59+
}
5560
if (isset($info['metadata'])) {
5661
printf('Metadata: %s', print_r($info['metadata'], true));
5762
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
/**
3+
* Copyright 2018 Google Inc.
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/api/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START storage_release_event_based_hold]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Releases an event-based hold for an object.
31+
*
32+
* @param string $bucketName the name of your Cloud Storage bucket.
33+
* @param string $objectName the name of your Cloud Storage object.
34+
*/
35+
function release_event_based_hold($bucketName, $objectName)
36+
{
37+
$storage = new StorageClient();
38+
$bucket = $storage->bucket($bucketName);
39+
$object = $bucket->object($objectName);
40+
$object->update(['eventBasedHold' => false]);
41+
printf('Event-based hold was released for %s' . PHP_EOL, $objectName);
42+
}
43+
# [END storage_release_event_based_hold]
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
/**
3+
* Copyright 2018 Google Inc.
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/api/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START storage_release_temporary_hold]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Releases a temporary hold for an object.
31+
*
32+
* @param string $bucketName the name of your Cloud Storage bucket.
33+
* @param string $objectName the name of your Cloud Storage object.
34+
*/
35+
function release_temporary_hold($bucketName, $objectName)
36+
{
37+
$storage = new StorageClient();
38+
$bucket = $storage->bucket($bucketName);
39+
$object = $bucket->object($objectName);
40+
$object->update(['temporaryHold' => false]);
41+
printf('Temporary hold was released for %s' . PHP_EOL, $objectName);
42+
}
43+
# [END storage_release_temporary_hold]

0 commit comments

Comments
 (0)