Skip to content

Commit f30c448

Browse files
authored
feat(Storage): Update samples to showcase Storage Autoclass V2 usage (#1928)
1 parent 461c088 commit f30c448

File tree

3 files changed

+39
-16
lines changed

3 files changed

+39
-16
lines changed

storage/src/get_bucket_autoclass.php

+6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ function get_bucket_autoclass(string $bucketName): void
4747
$bucketName,
4848
$info['autoclass']['toggleTime']
4949
);
50+
printf(
51+
'Autoclass terminal storage class is set to %s for %s at %s.' . PHP_EOL,
52+
$info['autoclass']['terminalStorageClass'],
53+
$info['name'],
54+
$info['autoclass']['terminalStorageClassUpdateTime'],
55+
);
5056
}
5157
}
5258
# [END storage_get_autoclass]

storage/src/set_bucket_autoclass.php

+15-7
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,38 @@
2727
use Google\Cloud\Storage\StorageClient;
2828

2929
/**
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.
30+
* Updates an existing bucket with provided autoclass config.
3431
*
3532
* @param string $bucketName The name of your Cloud Storage bucket (e.g. 'my-bucket').
3633
* @param bool $autoclassStatus If true, enables Autoclass. Disables otherwise.
34+
* @param string $terminalStorageClass This field is optional and defaults to `NEARLINE`.
35+
* Valid values are `NEARLINE` and `ARCHIVE`.
3736
*/
38-
function set_bucket_autoclass(string $bucketName, bool $autoclassStatus): void
39-
{
37+
function set_bucket_autoclass(
38+
string $bucketName,
39+
bool $autoclassStatus,
40+
string $terminalStorageClass
41+
): void {
4042
$storage = new StorageClient();
4143
$bucket = $storage->bucket($bucketName);
4244

4345
$bucket->update([
4446
'autoclass' => [
4547
'enabled' => $autoclassStatus,
48+
'terminalStorageClass' => $terminalStorageClass
4649
],
4750
]);
4851

52+
$info = $bucket->info();
4953
printf(
5054
'Updated bucket %s with autoclass set to %s.' . PHP_EOL,
51-
$bucketName,
55+
$info['name'],
5256
$autoclassStatus ? 'true' : 'false'
5357
);
58+
printf(
59+
'Autoclass terminal storage class is %s.' . PHP_EOL,
60+
$info['autoclass']['terminalStorageClass']
61+
);
5462
}
5563
# [END storage_set_autoclass]
5664

storage/test/storageTest.php

+18-9
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,7 @@ public function testGetBucketWithAutoclass()
836836
$bucket = self::$storage->createBucket($bucketName, [
837837
'autoclass' => [
838838
'enabled' => true,
839+
'terminalStorageClass' => 'ARCHIVE',
839840
],
840841
'location' => 'US',
841842
]);
@@ -849,30 +850,38 @@ public function testGetBucketWithAutoclass()
849850
sprintf('Bucket %s has autoclass enabled: %s', $bucketName, true),
850851
$output
851852
);
853+
$this->assertStringContainsString(
854+
sprintf('Autoclass terminal storage class is set to %s', 'ARCHIVE'),
855+
$output
856+
);
852857
}
853858

854859
public function testSetBucketWithAutoclass()
855860
{
856861
$bucket = self::$storage->createBucket(uniqid('samples-set-autoclass-'), [
857-
'autoclass' => [
858-
'enabled' => true,
859-
],
860862
'location' => 'US',
861863
]);
862-
$info = $bucket->reload();
863-
$this->assertArrayHasKey('autoclass', $info);
864-
$this->assertTrue($info['autoclass']['enabled']);
865864

865+
$terminalStorageClass = 'ARCHIVE';
866866
$output = self::runFunctionSnippet('set_bucket_autoclass', [
867867
$bucket->name(),
868-
false
868+
true,
869+
$terminalStorageClass
869870
]);
870871
$bucket->delete();
871872

872873
$this->assertStringContainsString(
873874
sprintf(
874-
'Updated bucket %s with autoclass set to false.',
875-
$bucket->name(),
875+
'Updated bucket %s with autoclass set to true.',
876+
$bucket->name()
877+
),
878+
$output
879+
);
880+
881+
$this->assertStringContainsString(
882+
sprintf(
883+
'Autoclass terminal storage class is %s.' . PHP_EOL,
884+
$terminalStorageClass
876885
),
877886
$output
878887
);

0 commit comments

Comments
 (0)