Skip to content

Commit 533a5d1

Browse files
feat: [Storage] a sample to set the endpoint (GoogleCloudPlatform#1608)
1 parent ffbf1f7 commit 533a5d1

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

storage/src/set_client_endpoint.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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_set_client_endpoint]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Sets a custom endpoint for storage client.
31+
*
32+
* @param string $projectId The ID of your Google Cloud Platform project.
33+
* @param string $endpoint The endpoint for storage client to target.
34+
*/
35+
function set_client_endpoint(
36+
string $projectId,
37+
string $endpoint
38+
): void {
39+
// $projectId = 'my-project-id';
40+
// $endpoint = 'https://storage.googleapis.com';
41+
42+
$storage = new StorageClient([
43+
'projectId' => $projectId,
44+
'apiEndpoint' => $endpoint,
45+
]);
46+
47+
// fetching apiEndpoint and baseUri from StorageClient is excluded for brevity
48+
# [START_EXCLUDE]
49+
$connectionProperty = new \ReflectionProperty($storage, 'connection');
50+
$connectionProperty->setAccessible(true);
51+
$connection = $connectionProperty->getValue($storage);
52+
53+
$apiEndpointProperty = new \ReflectionProperty($connection, 'apiEndpoint');
54+
$apiEndpointProperty->setAccessible(true);
55+
$apiEndpoint = $apiEndpointProperty->getValue($connection);
56+
57+
$requestBuilderProperty = new \ReflectionProperty($connection, 'requestBuilder');
58+
$requestBuilderProperty->setAccessible(true);
59+
$requestBuilder = $requestBuilderProperty->getValue($connection);
60+
61+
$baseUriProperty = new \ReflectionProperty($requestBuilder, 'baseUri');
62+
$baseUriProperty->setAccessible(true);
63+
$baseUri = $baseUriProperty->getValue($requestBuilder);
64+
65+
printf('API endpoint: %s' . PHP_EOL, $apiEndpoint);
66+
printf('Base URI: %s' . PHP_EOL, $baseUri);
67+
# [END_EXCLUDE]
68+
print('Storage Client initialized.' . PHP_EOL);
69+
}
70+
# [END storage_set_client_endpoint]
71+
72+
// The following 2 lines are only needed to run the samples
73+
require_once __DIR__ . '/../../testing/sample_helpers.php';
74+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

storage/test/storageTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,20 @@ public function testDownloadPublicObject()
804804
$this->assertFileExists($downloadTo);
805805
}
806806

807+
public function testSetClientEndpoint()
808+
{
809+
$testEndpoint = 'https://test-endpoint.com';
810+
811+
$output = self::runFunctionSnippet('set_client_endpoint', [
812+
self::$projectId,
813+
$testEndpoint,
814+
]);
815+
816+
$this->assertStringContainsString(sprintf('API endpoint: %s', $testEndpoint), $output);
817+
$this->assertStringContainsString(sprintf('Base URI: %s/storage/v1/', $testEndpoint), $output);
818+
$this->assertStringContainsString('Storage Client initialized.', $output);
819+
}
820+
807821
private function keyName()
808822
{
809823
return sprintf(

0 commit comments

Comments
 (0)