Skip to content

Commit 81498af

Browse files
feat(storage): sample for autoclass (GoogleCloudPlatform#1685)
1 parent 660a460 commit 81498af

File tree

3 files changed

+163
-0
lines changed

3 files changed

+163
-0
lines changed

storage/src/get_bucket_autoclass.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
/**
3+
* Copyright 2022 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_get_autoclass]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Print a bucket autoclass configuration.
31+
*
32+
* @param string $bucketName The name of your Cloud Storage bucket (e.g. 'my-bucket').
33+
*/
34+
function get_bucket_autoclass(string $bucketName): void
35+
{
36+
$storage = new StorageClient();
37+
$bucket = $storage->bucket($bucketName);
38+
39+
$info = $bucket->info();
40+
41+
if (isset($info['autoclass'])) {
42+
printf('Bucket %s has autoclass enabled: %s' . PHP_EOL,
43+
$bucketName,
44+
$info['autoclass']['enabled']
45+
);
46+
printf('Bucket %s has autoclass toggle time: %s' . PHP_EOL,
47+
$bucketName,
48+
$info['autoclass']['toggleTime']
49+
);
50+
}
51+
}
52+
# [END storage_get_autoclass]
53+
54+
// The following 2 lines are only needed to run the samples
55+
require_once __DIR__ . '/../../testing/sample_helpers.php';
56+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

storage/src/set_bucket_autoclass.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
/**
3+
* Copyright 2022 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_set_autoclass]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Updates an existing bucket with provided autoclass toggle.
31+
*
32+
* Note: Only patch requests that disable autoclass are currently supported.
33+
* To enable autoclass, it must be set at bucket creation time.
34+
*
35+
* @param string $bucketName The name of your Cloud Storage bucket (e.g. 'my-bucket').
36+
* @param bool $autoclassStatus If true, enables Autoclass. Disables otherwise.
37+
*/
38+
function set_bucket_autoclass(string $bucketName, bool $autoclassStatus): void
39+
{
40+
$storage = new StorageClient();
41+
$bucket = $storage->bucket($bucketName);
42+
43+
$bucket->update([
44+
'autoclass' => [
45+
'enabled' => $autoclassStatus,
46+
],
47+
]);
48+
49+
printf(
50+
'Updated bucket %s with autoclass set to %s.' . PHP_EOL,
51+
$bucketName,
52+
$autoclassStatus ? 'true' : 'false'
53+
);
54+
}
55+
# [END storage_set_autoclass]
56+
57+
// The following 2 lines are only needed to run the samples
58+
require_once __DIR__ . '/../../testing/sample_helpers.php';
59+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

storage/test/storageTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,54 @@ public function testChangeDefaultStorageClass()
792792
);
793793
}
794794

795+
public function testGetBucketWithAutoclass()
796+
{
797+
$bucketName = uniqid('samples-get-autoclass-');
798+
$bucket = self::$storage->createBucket($bucketName, [
799+
'autoclass' => [
800+
'enabled' => true,
801+
],
802+
'location' => 'US',
803+
]);
804+
805+
$output = self::runFunctionSnippet('get_bucket_autoclass', [
806+
$bucketName,
807+
]);
808+
$bucket->delete();
809+
810+
$this->assertStringContainsString(
811+
sprintf('Bucket %s has autoclass enabled: %s', $bucketName, true),
812+
$output
813+
);
814+
}
815+
816+
public function testSetBucketWithAutoclass()
817+
{
818+
$bucket = self::$storage->createBucket(uniqid('samples-set-autoclass-'), [
819+
'autoclass' => [
820+
'enabled' => true,
821+
],
822+
'location' => 'US',
823+
]);
824+
$info = $bucket->reload();
825+
$this->assertArrayHasKey('autoclass', $info);
826+
$this->assertTrue($info['autoclass']['enabled']);
827+
828+
$output = self::runFunctionSnippet('set_bucket_autoclass', [
829+
$bucket->name(),
830+
false
831+
]);
832+
$bucket->delete();
833+
834+
$this->assertStringContainsString(
835+
sprintf(
836+
'Updated bucket %s with autoclass set to false.',
837+
$bucket->name(),
838+
),
839+
$output
840+
);
841+
}
842+
795843
public function testDeleteFileArchivedGeneration()
796844
{
797845
$bucket = self::$storage->createBucket(uniqid('samples-delete-file-archived-generation-'), [

0 commit comments

Comments
 (0)