Skip to content

Commit ac48418

Browse files
samples: add samples for cloud storage buckets (#1931)
* chore: ignore samples/build/ * fix: convert string ports to number ports to match grpc * feat: provide redacted topic and subscription admin clients * tests: allow for storage bucket name gen * samples: add sample for subscription with cloud storage bucket * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * samples: remove gapic getters to be split to another PR * samples: update for review changes * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 34b8c03 commit ac48418

File tree

8 files changed

+258
-1
lines changed

8 files changed

+258
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-pubsub/tree
131131
| Create Push Subscription | [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/createPushSubscription.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/createPushSubscription.js,samples/README.md) |
132132
| Create Push Subscription With No Wrapper | [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/createPushSubscriptionNoWrapper.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/createPushSubscriptionNoWrapper.js,samples/README.md) |
133133
| Create Subscription | [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/createSubscription.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/createSubscription.js,samples/README.md) |
134+
| Create a Cloud Storage subscription | [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/createSubscriptionWithCloudStorage.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/createSubscriptionWithCloudStorage.js,samples/README.md) |
134135
| Create Subscription With Dead Letter Policy | [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/createSubscriptionWithDeadLetterPolicy.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/createSubscriptionWithDeadLetterPolicy.js,samples/README.md) |
135136
| Create an exactly-once delivery subscription | [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/createSubscriptionWithExactlyOnceDelivery.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/createSubscriptionWithExactlyOnceDelivery.js,samples/README.md) |
136137
| Create Subscription With Filtering | [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/createSubscriptionWithFiltering.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/createSubscriptionWithFiltering.js,samples/README.md) |

samples/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ guides.
2828
* [Create Push Subscription](#create-push-subscription)
2929
* [Create Push Subscription With No Wrapper](#create-push-subscription-with-no-wrapper)
3030
* [Create Subscription](#create-subscription)
31+
* [Create a Cloud Storage subscription](#create-a-cloud-storage-subscription)
3132
* [Create Subscription With Dead Letter Policy](#create-subscription-with-dead-letter-policy)
3233
* [Create an exactly-once delivery subscription](#create-an-exactly-once-delivery-subscription)
3334
* [Create Subscription With Filtering](#create-subscription-with-filtering)
@@ -253,6 +254,25 @@ __Usage:__
253254

254255

255256

257+
### Create a Cloud Storage subscription
258+
259+
Demonstrates how to create a subscription with Cloud Storage.
260+
261+
View the [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/createSubscriptionWithCloudStorage.js).
262+
263+
[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/createSubscriptionWithCloudStorage.js,samples/README.md)
264+
265+
__Usage:__
266+
267+
268+
`node createSubscriptionWithCloudStorage.js <topic-name> <subscription-name> <bucket> <filename-prefix> <filename-suffix> <max-duration>`
269+
270+
271+
-----
272+
273+
274+
275+
256276
### Create Subscription With Dead Letter Policy
257277

258278
Creates a new subscription With Dead Letter Policy.
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Copyright 2024 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+
// This is a generated sample, using the typeless sample bot. Please
16+
// look for the source TypeScript sample (.ts) for modifications.
17+
'use strict';
18+
19+
/**
20+
* This snippet demonstrates how to perform basic operations on
21+
* schemas with the Google Cloud Pub/Sub API.
22+
*
23+
* For more information, see the README.md under /pubsub and the documentation
24+
* at https://cloud.google.com/pubsub/docs.
25+
*/
26+
27+
// sample-metadata:
28+
// title: Create a Cloud Storage subscription
29+
// description: Demonstrates how to create a subscription with Cloud Storage.
30+
// usage: node createSubscriptionWithCloudStorage.js <topic-name> <subscription-name> <bucket> <filename-prefix> <filename-suffix> <max-duration>
31+
32+
// [START pubsub_create_cloud_storage_subscription]
33+
/**
34+
* TODO(developer): Uncomment these variables before running the sample.
35+
*/
36+
// const topicName = 'YOUR_TOPIC_NAME';
37+
// const subscriptionName = 'YOUR_SUBSCRIPTION_NAME';
38+
// const bucket = 'YOUR_BUCKET_ID';
39+
// const filenamePrefix = 'YOUR_FILENAME_PREFIX';
40+
// const filenameSuffix = 'YOUR_FILENAME_SUFFIX';
41+
// const maxDuration = 60;
42+
43+
// Imports the Google Cloud client library
44+
const {PubSub} = require('@google-cloud/pubsub');
45+
46+
// Creates a client; cache this for further use
47+
const pubSubClient = new PubSub();
48+
49+
async function createCloudStorageSubscription(
50+
topicName,
51+
subscriptionName,
52+
bucket,
53+
filenamePrefix,
54+
filenameSuffix,
55+
maxDuration
56+
) {
57+
const options = {
58+
cloudStorageConfig: {
59+
bucket,
60+
filenamePrefix,
61+
filenameSuffix,
62+
maxDuration: {
63+
seconds: maxDuration,
64+
},
65+
},
66+
};
67+
68+
await pubSubClient
69+
.topic(topicName)
70+
.createSubscription(subscriptionName, options);
71+
72+
console.log(
73+
`Created subscription ${subscriptionName} with a cloud storage configuration.`
74+
);
75+
}
76+
// [END pubsub_create_cloud_storage_subscription]
77+
78+
function main(
79+
topicName = 'YOUR_TOPIC_NAME',
80+
subscriptionName = 'YOUR_SUBSCRIPTION_NAME',
81+
bucket = 'YOUR_BUCKET_NAME',
82+
filenamePrefix = 'YOUR_FILENAME_PREFIX',
83+
filenameSuffix = 'YOUR_FILENAME_SUFFIX',
84+
maxDuration = 60
85+
) {
86+
createCloudStorageSubscription(
87+
topicName,
88+
subscriptionName,
89+
bucket,
90+
filenamePrefix,
91+
filenameSuffix,
92+
maxDuration
93+
).catch(err => {
94+
console.error(err.message);
95+
process.exitCode = 1;
96+
});
97+
}
98+
99+
main(...process.argv.slice(2));

samples/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
},
2323
"dependencies": {
2424
"@google-cloud/pubsub": "^4.5.0",
25+
"@google-cloud/storage": "^7.11.1",
2526
"@opentelemetry/api": "^1.6.0",
2627
"@opentelemetry/tracing": "^0.24.0",
2728
"avro-js": "^1.11.3",

samples/system-test/subscriptions.test.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
import {BigQuery} from '@google-cloud/bigquery';
16+
import {Bucket, Storage} from '@google-cloud/storage';
1617
import {
1718
CreateSubscriptionOptions,
1819
PubSub,
@@ -74,7 +75,7 @@ describe('subscriptions', () => {
7475

7576
async function createBigQueryTable(datasetId: string, tableId: string) {
7677
const bigquery = new BigQuery({
77-
projectId: projectId,
78+
projectId,
7879
});
7980

8081
const datasetOptions = {
@@ -108,6 +109,17 @@ describe('subscriptions', () => {
108109
await bigquery.dataset(datasetId).delete(deleteOptions);
109110
}
110111

112+
async function createStorageBucket(testName: string): Promise<Bucket> {
113+
const storage = new Storage({
114+
projectId,
115+
});
116+
117+
const name = resources.generateStorageName(testName);
118+
119+
const [bucket] = await storage.createBucket(name);
120+
return bucket;
121+
}
122+
111123
async function cleanSubs() {
112124
const [subscriptions] = await pubsub.getSubscriptions();
113125
await Promise.all(
@@ -428,6 +440,24 @@ describe('subscriptions', () => {
428440
assert(subscriptionDetached === true);
429441
});
430442

443+
it('should create a subscription with a cloud storage config', async () => {
444+
const testId = 'sub_storage';
445+
const topic = await createTopic(testId);
446+
const subName = reserveSub(testId);
447+
const bucket = await createStorageBucket(testId);
448+
const bucketName = bucket.name;
449+
try {
450+
const output = execSync(
451+
`${commandFor('createSubscriptionWithCloudStorage')} ${
452+
topic.name
453+
} projects/${projectId}/subscriptions/${subName} ${bucketName} 'prefix' 'suffix' 60`
454+
);
455+
assert.include(output, 'Created subscription');
456+
} finally {
457+
bucket.delete();
458+
}
459+
});
460+
431461
it('should create a subscription with dead letter policy.', async () => {
432462
const testId = 'dead_letter';
433463
const topic = await createTopic(testId),

samples/system-test/testResources.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@ export class TestResources {
101101
);
102102
}
103103

104+
/**
105+
* Generates a unique resource name for one run of a test within
106+
* a test suite for Cloud Storage resources.
107+
*/
108+
generateStorageName(testId: string): string {
109+
return [normalizeId(this.getPrefix(testId)), this.tokenMaker.uuid()].join(
110+
'_'
111+
);
112+
}
113+
104114
/*!
105115
* Given a list of resource names (and a test ID), this will return
106116
* a list of all resources that should be deleted to clean up for
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Copyright 2024 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+
/**
16+
* This snippet demonstrates how to perform basic operations on
17+
* schemas with the Google Cloud Pub/Sub API.
18+
*
19+
* For more information, see the README.md under /pubsub and the documentation
20+
* at https://cloud.google.com/pubsub/docs.
21+
*/
22+
23+
// sample-metadata:
24+
// title: Create a Cloud Storage subscription
25+
// description: Demonstrates how to create a subscription with Cloud Storage.
26+
// usage: node createSubscriptionWithCloudStorage.js <topic-name> <subscription-name> <bucket> <filename-prefix> <filename-suffix> <max-duration>
27+
28+
// [START pubsub_create_cloud_storage_subscription]
29+
/**
30+
* TODO(developer): Uncomment these variables before running the sample.
31+
*/
32+
// const topicName = 'YOUR_TOPIC_NAME';
33+
// const subscriptionName = 'YOUR_SUBSCRIPTION_NAME';
34+
// const bucket = 'YOUR_BUCKET_ID';
35+
// const filenamePrefix = 'YOUR_FILENAME_PREFIX';
36+
// const filenameSuffix = 'YOUR_FILENAME_SUFFIX';
37+
// const maxDuration = 60;
38+
39+
// Imports the Google Cloud client library
40+
import {CreateSubscriptionOptions, PubSub} from '@google-cloud/pubsub';
41+
42+
// Creates a client; cache this for further use
43+
const pubSubClient = new PubSub();
44+
45+
async function createCloudStorageSubscription(
46+
topicName: string,
47+
subscriptionName: string,
48+
bucket: string,
49+
filenamePrefix: string,
50+
filenameSuffix: string,
51+
maxDuration: number
52+
) {
53+
const options: CreateSubscriptionOptions = {
54+
cloudStorageConfig: {
55+
bucket,
56+
filenamePrefix,
57+
filenameSuffix,
58+
maxDuration: {
59+
seconds: maxDuration,
60+
},
61+
},
62+
};
63+
64+
await pubSubClient
65+
.topic(topicName)
66+
.createSubscription(subscriptionName, options);
67+
68+
console.log(
69+
`Created subscription ${subscriptionName} with a cloud storage configuration.`
70+
);
71+
}
72+
// [END pubsub_create_cloud_storage_subscription]
73+
74+
function main(
75+
topicName = 'YOUR_TOPIC_NAME',
76+
subscriptionName = 'YOUR_SUBSCRIPTION_NAME',
77+
bucket = 'YOUR_BUCKET_NAME',
78+
filenamePrefix = 'YOUR_FILENAME_PREFIX',
79+
filenameSuffix = 'YOUR_FILENAME_SUFFIX',
80+
maxDuration = 60
81+
) {
82+
createCloudStorageSubscription(
83+
topicName,
84+
subscriptionName,
85+
bucket,
86+
filenamePrefix,
87+
filenameSuffix,
88+
maxDuration
89+
).catch(err => {
90+
console.error(err.message);
91+
process.exitCode = 1;
92+
});
93+
}
94+
95+
main(...process.argv.slice(2));

src/pubsub.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ export class PubSub {
307307
allScopes[scope] = true;
308308
}
309309
}
310+
310311
this.options = Object.assign(
311312
{
312313
libName: 'gccl',

0 commit comments

Comments
 (0)