-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpublisher.py
More file actions
24 lines (20 loc) · 760 Bytes
/
publisher.py
File metadata and controls
24 lines (20 loc) · 760 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from google.cloud import pubsub_v1
import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = "project_key_example.json"
# TODO(developer)
project_id = "crucial-matter-292203"
topic_id = "hello_topic"
publisher = pubsub_v1.PublisherClient()
# The `topic_path` method creates a fully qualified identifier
# in the form `projects/{project_id}/topics/{topic_id}`
topic_path = publisher.topic_path(project_id, topic_id)
for n in range(1, 10):
data = "Message number {}".format(n)
print("TRY")
print(n)
# Data must be a bytestring
data = data.encode("utf-8")
# When you publish a message, the client returns a future.
future = publisher.publish(topic_path, data)
print(future.result())
print(f"Published messages to {topic_path}.")