Skip to content

Commit d79165f

Browse files
committed
feat(storage): introduce quickstart sample and test for migration
1 parent 3b66abe commit d79165f

9 files changed

Lines changed: 166 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/quickstart.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2019 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+
function main(bucketName = 'my-new-bucket') {
18+
// [START storage_quickstart]
19+
// Imports the Google Cloud client library
20+
const {Storage} = require('@google-cloud/storage');
21+
22+
// For more information on ways to initialize Storage, please see
23+
// https://googleapis.dev/nodejs/storage/latest/Storage.html
24+
25+
// Creates a client using Application Default Credentials
26+
const storage = new Storage();
27+
28+
// Creates a client from a Google service account key
29+
// const storage = new Storage({keyFilename: 'key.json'});
30+
31+
/**
32+
* TODO(developer): Uncomment these variables before running the sample.
33+
*/
34+
// The ID of your GCS bucket
35+
// const bucketName = 'your-unique-bucket-name';
36+
37+
async function createBucket() {
38+
try {
39+
// Creates the new bucket
40+
await storage.createBucket(bucketName);
41+
console.log(`Bucket ${bucketName} created.`);
42+
} catch (error) {
43+
console.error('Error executing create bucket:', error.message || error);
44+
}
45+
}
46+
47+
createBucket();
48+
// [END storage_quickstart]
49+
}
50+
51+
main(...process.argv.slice(2));

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+
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2019 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 {after, it} = require('mocha');
19+
const cp = require('child_process');
20+
const uuid = require('uuid');
21+
const {Storage} = require('@google-cloud/storage');
22+
23+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
24+
25+
const storage = new Storage();
26+
const bucketName = `nodejs-storage-samples-${uuid.v4()}`;
27+
28+
after(async () => {
29+
const bucket = storage.bucket(bucketName);
30+
await bucket.delete().catch(console.error);
31+
});
32+
33+
it('should run the quickstart', async () => {
34+
const stdout = execSync(`node quickstart ${bucketName}`);
35+
assert.match(stdout, /Bucket .* created./);
36+
});

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

Whitespace-only changes.

0 commit comments

Comments
 (0)