Skip to content

Commit ca3f772

Browse files
committed
feat(storage): introduce notification samples and tests for migration
1 parent 3b66abe commit ca3f772

12 files changed

Lines changed: 428 additions & 0 deletions

storage/createNotification.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2020 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 how to perform basic operations on files 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+
const uuid = require('uuid');
25+
26+
function main(
27+
bucketName = 'my-bucket',
28+
topic = `nodejs-storage-samples-${uuid.v4()}`
29+
) {
30+
// [START storage_create_bucket_notifications]
31+
/**
32+
* TODO(developer): Uncomment the following lines before running the sample.
33+
*/
34+
// The ID of your GCS bucket
35+
// const bucketName = 'your-unique-bucket-name';
36+
37+
// The name of a topic
38+
// const topic = 'my-topic';
39+
40+
// Imports the Google Cloud client library
41+
const {Storage} = require('@google-cloud/storage');
42+
43+
// Creates a client
44+
const storage = new Storage();
45+
46+
async function createNotification() {
47+
try {
48+
// Creates a notification
49+
await storage.bucket(bucketName).createNotification(topic);
50+
51+
console.log('Notification subscription created.');
52+
} catch (error) {
53+
console.error(
54+
'Error executing create notification:',
55+
error.message || error
56+
);
57+
}
58+
}
59+
60+
createNotification();
61+
// [END storage_create_bucket_notifications]
62+
}
63+
main(...process.argv.slice(2));

storage/deleteNotification.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2020 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 how to perform basic operations on files 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(bucketName = 'my-bucket', notificationId = '1') {
26+
// [START storage_delete_bucket_notification]
27+
/**
28+
* TODO(developer): Uncomment the following lines before running the sample.
29+
*/
30+
// The ID of your GCS bucket
31+
// const bucketName = 'your-unique-bucket-name';
32+
33+
// The ID of the notification
34+
// const notificationId = '1';
35+
36+
// Imports the Google Cloud client library
37+
const {Storage} = require('@google-cloud/storage');
38+
39+
// Creates a client
40+
const storage = new Storage();
41+
42+
async function deleteNotification() {
43+
try {
44+
// Deletes the notification from the bucket
45+
await storage.bucket(bucketName).notification(notificationId).delete();
46+
47+
console.log(`Notification ${notificationId} deleted.`);
48+
} catch (error) {
49+
console.error(
50+
'Error executing delete notification:',
51+
error.message || error
52+
);
53+
}
54+
}
55+
56+
deleteNotification();
57+
// [END storage_delete_bucket_notification]
58+
}
59+
main(...process.argv.slice(2));
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2020 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 how to perform basic operations on files 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(bucketName = 'my-bucket', notificationId = '1') {
26+
// [START storage_print_pubsub_bucket_notification]
27+
/**
28+
* TODO(developer): Uncomment the following lines before running the sample.
29+
*/
30+
// The ID of your GCS bucket
31+
// const bucketName = 'your-unique-bucket-name';
32+
33+
// The ID of the notification
34+
// const notificationId = '1';
35+
36+
// Imports the Google Cloud client library
37+
const {Storage} = require('@google-cloud/storage');
38+
39+
// Creates a client
40+
const storage = new Storage();
41+
42+
async function getMetadata() {
43+
try {
44+
// Get the notification metadata
45+
const [metadata] = await storage
46+
.bucket(bucketName)
47+
.notification(notificationId)
48+
.getMetadata();
49+
50+
console.log(`ID: ${metadata.id}`);
51+
console.log(`Topic: ${metadata.topic}`);
52+
console.log(`Event Types: ${metadata.event_types}`);
53+
console.log(`Custom Attributes: ${metadata.custom_attributes}`);
54+
console.log(`Payload Format: ${metadata.payload_format}`);
55+
console.log(`Object Name Prefix: ${metadata.object_name_prefix}`);
56+
console.log(`Etag: ${metadata.etag}`);
57+
console.log(`Self Link: ${metadata.selfLink}`);
58+
console.log(`Kind: ${metadata.kind}`);
59+
} catch (error) {
60+
console.error('Error executing get metadata:', error.message || error);
61+
}
62+
}
63+
64+
getMetadata();
65+
// [END storage_print_pubsub_bucket_notification]
66+
}
67+
main(...process.argv.slice(2));

storage/listNotifications.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2020 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 how to perform basic operations on files 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(bucketName = 'my-bucket') {
26+
// [START storage_list_bucket_notifications]
27+
/**
28+
* TODO(developer): Uncomment the following lines before running the sample.
29+
*/
30+
// The ID of your GCS bucket
31+
// const bucketName = 'your-unique-bucket-name';
32+
33+
// Imports the Google Cloud client library
34+
const {Storage} = require('@google-cloud/storage');
35+
36+
// Creates a client
37+
const storage = new Storage();
38+
39+
async function listNotifications() {
40+
try {
41+
// Lists notifications in the bucket
42+
const [notifications] = await storage
43+
.bucket(bucketName)
44+
.getNotifications();
45+
46+
console.log('Notifications:');
47+
notifications.forEach(notification => {
48+
console.log(notification.id);
49+
});
50+
} catch (error) {
51+
console.error(
52+
'Error executing list notifications:',
53+
error.message || error
54+
);
55+
}
56+
}
57+
58+
listNotifications();
59+
// [END storage_list_bucket_notifications]
60+
}
61+
main(...process.argv.slice(2));

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

0 commit comments

Comments
 (0)