Complete the code to create a Pub/Sub client in Python.
from google.cloud import pubsub_v1 publisher = pubsub_v1.PublisherClient() topic_path = publisher.topic_path('my-project', [1])
The topic_path method requires the project ID and the topic name. Here, 'my-topic' is the topic name.
Complete the code to publish a message to the topic.
data = 'Hello, Cloud!'.encode('utf-8') publisher.publish(topic_path, [1]=data)
The publish method expects the message data as the data parameter.
Fix the error in the code to correctly publish a message with attributes.
attributes = {'origin': 'python-sample'}
publisher.publish(topic_path, data=data, [1]=attributes)The publish method uses the attributes parameter to send message attributes as a dictionary.
Fill both blanks to create a topic path and publish a message with attributes.
topic_path = publisher.[1]('my-project', 'my-topic') data = 'Test message'.encode('utf-8') publisher.publish(topic_path, data=data, [2]={'source': 'test'})
The method to create a topic path is topic_path, and the message attributes parameter is attributes.
Fill all three blanks to create a publisher client, build a topic path, and publish a message with attributes.
from google.cloud import pubsub_v1 publisher = pubsub_v1.[1]() topic_path = publisher.[2]('project-123', 'events') data = 'Event data'.encode('utf-8') publisher.publish(topic_path, data=data, [3]={'env': 'prod'})
SubscriberClient instead of PublisherClient.The client class is PublisherClient, the method to build the topic path is topic_path, and the attributes parameter is attributes.