Skip to content

Commit 3eb8db5

Browse files
committed
feat(storage): introduce storage client endpoint sample and test for migration
1 parent 3b66abe commit 3eb8db5

9 files changed

Lines changed: 162 additions & 0 deletions

File tree

storage/package.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "@google-cloud/storage-samples",
3+
"description": "Samples for the Cloud Storage Client Library for Node.js.",
4+
"license": "Apache-2.0",
5+
"author": "Google Inc.",
6+
"engines": {
7+
"node": ">=12"
8+
},
9+
"repository": "googleapis/nodejs-storage",
10+
"private": true,
11+
"files": [
12+
"*.js"
13+
],
14+
"scripts": {
15+
"cleanup": "node scripts/cleanup",
16+
"test": "mocha system-test/*.js --timeout 800000"
17+
},
18+
"dependencies": {
19+
"@google-cloud/pubsub": "^4.0.0",
20+
"@google-cloud/storage": "^7.19.0",
21+
"node-fetch": "^2.6.7",
22+
"uuid": "^8.0.0",
23+
"yargs": "^16.0.0"
24+
},
25+
"devDependencies": {
26+
"chai": "^4.2.0",
27+
"mocha": "^8.0.0",
28+
"p-limit": "^3.1.0"
29+
}
30+
}

storage/resources/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
downloaded.txt
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Sub1
2+
Hello World!

storage/resources/test.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello World!

storage/resources/test2.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello World 2!

storage/scripts/cleanup

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env node
2+
3+
// Copyright 2019 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+
'use strict';
18+
19+
const {Storage} = require('@google-cloud/storage');
20+
const storage = new Storage();
21+
const NAME_REG_EXP = /^nodejs-storage-samples-[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}$/;
22+
23+
storage
24+
.getBuckets()
25+
.then(([buckets]) => {
26+
let promise = Promise.resolve();
27+
28+
buckets
29+
.filter((bucket) => NAME_REG_EXP.test(bucket.name))
30+
.forEach((bucket) => {
31+
promise = promise.then(() => {
32+
return bucket.deleteFiles()
33+
.then(() => bucket.deleteFiles(), console.error)
34+
.then(() => {
35+
console.log(`Deleting ${bucket.name}`);
36+
return bucket.delete();
37+
}, console.error)
38+
.catch(console.error);
39+
});
40+
});
41+
})
42+
.catch((err) => {
43+
console.error('ERROR:', err);
44+
});

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+
});

storage/system-test/test_9d800329-00da-4cdd-9a3e-7ac6743d5813.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)