Skip to content

Commit 6114e32

Browse files
angelcaamaliennae
andauthored
feat(storage): introduce storage client endpoint sample and test for migration (#4263)
Co-authored-by: Jennifer Davis <sigje@google.com>
1 parent 18dbc6b commit 6114e32

2 files changed

Lines changed: 83 additions & 0 deletions

File tree

storage/setClientEndpoint.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
/**
18+
* This application demonstrates set a custom endpoint with
19+
* the Google Cloud Storage API.
20+
*
21+
* For more information, see the README.md under /storage and the documentation
22+
* at https://cloud.google.com/storage/docs.
23+
*/
24+
25+
function main(apiEndpoint = 'https://storage.googleapis.com') {
26+
// [START storage_set_client_endpoint]
27+
/**
28+
* TODO(developer): Uncomment the following lines before running the sample.
29+
*/
30+
// The custom endpoint to which requests should be made
31+
// const apiEndpoint = 'https://yourcustomendpoint.com';
32+
33+
// Imports the Google Cloud client library
34+
const {Storage} = require('@google-cloud/storage');
35+
try {
36+
// Creates a client
37+
const storage = new Storage({
38+
apiEndpoint: apiEndpoint,
39+
useAuthWithCustomEndpoint: true,
40+
});
41+
42+
console.log(`Client initiated with endpoint: ${storage.apiEndpoint}.`);
43+
} catch (error) {
44+
console.error(
45+
'Error executing set client endpoint:',
46+
error.message || error
47+
);
48+
}
49+
50+
// [END storage_set_client_endpoint]
51+
}
52+
53+
main(...process.argv.slice(2));
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
const {assert} = require('chai');
18+
const {it} = require('mocha');
19+
const cp = require('child_process');
20+
21+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
22+
23+
it('should intialize storage with a custom api endpoint', async () => {
24+
const apiEndpoint = 'https://storage.googleapis.com';
25+
const output = execSync(`node setClientEndpoint.js ${apiEndpoint}`);
26+
assert.match(
27+
output,
28+
new RegExp(`Client initiated with endpoint: ${apiEndpoint}.`)
29+
);
30+
});

0 commit comments

Comments
 (0)