Skip to content

Commit a367721

Browse files
angelcaamaliennae
andauthored
feat(storage): migrate quickstart sample and tests (#4264)
* feat(storage): introduce quickstart sample and test for migration * test(storage): fix quickstart system test --------- Co-authored-by: Jennifer Davis <sigje@google.com>
1 parent 5d9f94c commit a367721

2 files changed

Lines changed: 87 additions & 0 deletions

File tree

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));
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({force: true}).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+
});

0 commit comments

Comments
 (0)