Skip to content

Commit 34de141

Browse files
authored
feat(Storage): Added sample to print bucket website configuration
* Added sample to print bucket website configuration for GCS.
1 parent 5c4fd35 commit 34de141

File tree

2 files changed

+85
-11
lines changed

2 files changed

+85
-11
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
/**
3+
* Copyright 2022 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/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START storage_print_bucket_website_configuration]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Print the website configuration for a Cloud Storage bucket.
31+
*
32+
* @param string $bucketName The name of your Cloud Storage bucket.
33+
*/
34+
function print_bucket_website_configuration(string $bucketName): void
35+
{
36+
$storage = new StorageClient();
37+
$bucket = $storage->bucket($bucketName);
38+
$info = $bucket->info();
39+
40+
if (!array_key_exists('website', $info)) {
41+
printf('Bucket website configuration not set' . PHP_EOL);
42+
} else {
43+
printf(
44+
'Index page: %s' . PHP_EOL . '404 page: %s' . PHP_EOL,
45+
$info['website']['mainPageSuffix'],
46+
$info['website']['notFoundPage'],
47+
);
48+
}
49+
}
50+
# [END storage_print_bucket_website_configuration]
51+
52+
// The following 2 lines are only needed to run the samples
53+
require_once __DIR__ . '/../../testing/sample_helpers.php';
54+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

storage/test/storageTest.php

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -474,28 +474,48 @@ public function testBucketWebsiteConfiguration()
474474
'name' => 'test.html'
475475
]);
476476

477+
$output = self::runFunctionSnippet('print_bucket_website_configuration', [
478+
$bucket->name(),
479+
]);
480+
481+
$this->assertEquals(
482+
sprintf('Bucket website configuration not set' . PHP_EOL),
483+
$output,
484+
);
485+
477486
$output = self::runFunctionSnippet('define_bucket_website_configuration', [
478487
$bucket->name(),
479488
$obj->name(),
480489
$obj->name(),
481490
]);
482491

492+
$this->assertEquals(
493+
sprintf(
494+
'Static website bucket %s is set up to use %s as the index page and %s as the 404 page.',
495+
$bucket->name(),
496+
$obj->name(),
497+
$obj->name(),
498+
),
499+
$output
500+
);
501+
483502
$info = $bucket->reload();
484-
$obj->delete();
485-
$bucket->delete();
503+
504+
$output = self::runFunctionSnippet('print_bucket_website_configuration', [
505+
$bucket->name(),
506+
]);
486507

487508
$this->assertEquals(
488-
sprintf(
489-
'Static website bucket %s is set up to use %s as the index page and %s as the 404 page.',
490-
$bucket->name(),
491-
$obj->name(),
492-
$obj->name(),
493-
),
494-
$output
509+
sprintf(
510+
'Index page: %s' . PHP_EOL . '404 page: %s' . PHP_EOL,
511+
$info['website']['mainPageSuffix'],
512+
$info['website']['notFoundPage'],
513+
),
514+
$output,
495515
);
496516

497-
$this->assertEquals($obj->name(), $info['website']['mainPageSuffix']);
498-
$this->assertEquals($obj->name(), $info['website']['notFoundPage']);
517+
$obj->delete();
518+
$bucket->delete();
499519
}
500520

501521
public function testGetServiceAccount()

0 commit comments

Comments
 (0)