本文整理汇总了Python中google.cloud.pubsub_v1.PublisherClient方法的典型用法代码示例。如果您正苦于以下问题:Python pubsub_v1.PublisherClient方法的具体用法?Python pubsub_v1.PublisherClient怎么用?Python pubsub_v1.PublisherClient使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类google.cloud.pubsub_v1
的用法示例。
在下文中一共展示了pubsub_v1.PublisherClient方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_grpc_request_with_regular_credentials
# 需要导入模块: from google.cloud import pubsub_v1 [as 别名]
# 或者: from google.cloud.pubsub_v1 import PublisherClient [as 别名]
def test_grpc_request_with_regular_credentials(http_request):
credentials, project_id = google.auth.default()
credentials = google.auth.credentials.with_scopes_if_required(
credentials, ["https://www.googleapis.com/auth/pubsub"]
)
transport = publisher_grpc_transport.PublisherGrpcTransport(
address=publisher_client.PublisherClient.SERVICE_ADDRESS,
credentials=credentials,
)
# Create a pub/sub client.
client = pubsub_v1.PublisherClient(transport=transport)
# list the topics and drain the iterator to test that an authorized API
# call works.
list_topics_iter = client.list_topics(project="projects/{}".format(project_id))
list(list_topics_iter)
示例2: test_grpc_request_with_jwt_credentials
# 需要导入模块: from google.cloud import pubsub_v1 [as 别名]
# 或者: from google.cloud.pubsub_v1 import PublisherClient [as 别名]
def test_grpc_request_with_jwt_credentials():
credentials, project_id = google.auth.default()
audience = "https://pubsub.googleapis.com/google.pubsub.v1.Publisher"
credentials = google.auth.jwt.Credentials.from_signing_credentials(
credentials, audience=audience
)
transport = publisher_grpc_transport.PublisherGrpcTransport(
address=publisher_client.PublisherClient.SERVICE_ADDRESS,
credentials=credentials,
)
# Create a pub/sub client.
client = pubsub_v1.PublisherClient(transport=transport)
# list the topics and drain the iterator to test that an authorized API
# call works.
list_topics_iter = client.list_topics(project="projects/{}".format(project_id))
list(list_topics_iter)
示例3: test_grpc_request_with_on_demand_jwt_credentials
# 需要导入模块: from google.cloud import pubsub_v1 [as 别名]
# 或者: from google.cloud.pubsub_v1 import PublisherClient [as 别名]
def test_grpc_request_with_on_demand_jwt_credentials():
credentials, project_id = google.auth.default()
credentials = google.auth.jwt.OnDemandCredentials.from_signing_credentials(
credentials
)
transport = publisher_grpc_transport.PublisherGrpcTransport(
address=publisher_client.PublisherClient.SERVICE_ADDRESS,
credentials=credentials,
)
# Create a pub/sub client.
client = pubsub_v1.PublisherClient(transport=transport)
# list the topics and drain the iterator to test that an authorized API
# call works.
list_topics_iter = client.list_topics(project="projects/{}".format(project_id))
list(list_topics_iter)
示例4: pub
# 需要导入模块: from google.cloud import pubsub_v1 [as 别名]
# 或者: from google.cloud.pubsub_v1 import PublisherClient [as 别名]
def pub(project_id, topic_id):
"""Publishes a message to a Pub/Sub topic."""
# [START pubsub_quickstart_pub_client]
# Initialize a Publisher client.
client = pubsub_v1.PublisherClient()
# [END pubsub_quickstart_pub_client]
# Create a fully qualified identifier in the form of
# `projects/{project_id}/topics/{topic_id}`
topic_path = client.topic_path(project_id, topic_id)
# Data sent to Cloud Pub/Sub must be a bytestring.
data = b"Hello, World!"
# Keep track of the number of published messages.
ref = dict({"num_messages": 0})
# When you publish a message, the client returns a future.
api_future = client.publish(topic_path, data=data)
api_future.add_done_callback(get_callback(api_future, data, ref))
# Keep the main thread from exiting while the message future
# gets resolved in the background.
while api_future.running():
time.sleep(0.5)
print("Published {} message(s).".format(ref["num_messages"]))
示例5: get_topic_policy
# 需要导入模块: from google.cloud import pubsub_v1 [as 别名]
# 或者: from google.cloud.pubsub_v1 import PublisherClient [as 别名]
def get_topic_policy(project, topic_id):
"""Prints the IAM policy for the given topic."""
# [START pubsub_get_topic_policy]
from google.cloud import pubsub_v1
# TODO(developer)
# project_id = "your-project-id"
# topic_id = "your-topic-id"
client = pubsub_v1.PublisherClient()
topic_path = client.topic_path(project, topic_id)
policy = client.get_iam_policy(topic_path)
print("Policy for topic {}:".format(topic_path))
for binding in policy.bindings:
print("Role: {}, Members: {}".format(binding.role, binding.members))
# [END pubsub_get_topic_policy]
示例6: check_topic_permissions
# 需要导入模块: from google.cloud import pubsub_v1 [as 别名]
# 或者: from google.cloud.pubsub_v1 import PublisherClient [as 别名]
def check_topic_permissions(project, topic_id):
"""Checks to which permissions are available on the given topic."""
# [START pubsub_test_topic_permissions]
from google.cloud import pubsub_v1
# TODO(developer)
# project_id = "your-project-id"
# topic_id = "your-topic-id"
client = pubsub_v1.PublisherClient()
topic_path = client.topic_path(project, topic_id)
permissions_to_check = ["pubsub.topics.publish", "pubsub.topics.update"]
allowed_permissions = client.test_iam_permissions(topic_path, permissions_to_check)
print(
"Allowed permissions for topic {}: {}".format(topic_path, allowed_permissions)
)
# [END pubsub_test_topic_permissions]
示例7: create_topic
# 需要导入模块: from google.cloud import pubsub_v1 [as 别名]
# 或者: from google.cloud.pubsub_v1 import PublisherClient [as 别名]
def create_topic(project_id, topic_id):
"""Create a new Pub/Sub topic."""
# [START pubsub_quickstart_create_topic]
# [START pubsub_create_topic]
from google.cloud import pubsub_v1
# TODO(developer)
# project_id = "your-project-id"
# topic_id = "your-topic-id"
publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path(project_id, topic_id)
topic = publisher.create_topic(topic_path)
print("Topic created: {}".format(topic))
# [END pubsub_quickstart_create_topic]
# [END pubsub_create_topic]
示例8: delete_topic
# 需要导入模块: from google.cloud import pubsub_v1 [as 别名]
# 或者: from google.cloud.pubsub_v1 import PublisherClient [as 别名]
def delete_topic(project_id, topic_id):
"""Deletes an existing Pub/Sub topic."""
# [START pubsub_delete_topic]
from google.cloud import pubsub_v1
# TODO(developer)
# project_id = "your-project-id"
# topic_id = "your-topic-id"
publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path(project_id, topic_id)
publisher.delete_topic(topic_path)
print("Topic deleted: {}".format(topic_path))
# [END pubsub_delete_topic]
示例9: publish_messages_with_custom_attributes
# 需要导入模块: from google.cloud import pubsub_v1 [as 别名]
# 或者: from google.cloud.pubsub_v1 import PublisherClient [as 别名]
def publish_messages_with_custom_attributes(project_id, topic_id):
"""Publishes multiple messages with custom attributes
to a Pub/Sub topic."""
# [START pubsub_publish_custom_attributes]
from google.cloud import pubsub_v1
# TODO(developer)
# project_id = "your-project-id"
# topic_id = "your-topic-id"
publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path(project_id, topic_id)
for n in range(1, 10):
data = u"Message number {}".format(n)
# Data must be a bytestring
data = data.encode("utf-8")
# Add two attributes, origin and username, to the message
future = publisher.publish(
topic_path, data, origin="python-sample", username="gcp"
)
print(future.result())
print("Published messages with custom attributes.")
# [END pubsub_publish_custom_attributes]
示例10: index
# 需要导入模块: from google.cloud import pubsub_v1 [as 别名]
# 或者: from google.cloud.pubsub_v1 import PublisherClient [as 别名]
def index():
if request.method == 'GET':
return render_template('index.html', messages=MESSAGES)
data = request.form.get('payload', 'Example payload').encode('utf-8')
# publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path(
current_app.config['PROJECT'],
current_app.config['PUBSUB_TOPIC'])
publisher.publish(topic_path, data=data)
return 'OK', 200
# [END gae_flex_pubsub_index]
# [START gae_flex_pubsub_push]
示例11: index
# 需要导入模块: from google.cloud import pubsub_v1 [as 别名]
# 或者: from google.cloud.pubsub_v1 import PublisherClient [as 别名]
def index():
if request.method == 'GET':
return render_template('index.html', messages=MESSAGES, tokens=TOKENS,
claims=CLAIMS)
data = request.form.get('payload', 'Example payload').encode('utf-8')
# Consider initializing the publisher client outside this function
# for better latency performance.
publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path(app.config['GOOGLE_CLOUD_PROJECT'],
app.config['PUBSUB_TOPIC'])
future = publisher.publish(topic_path, data)
future.result()
return 'OK', 200
# [END index]
# [START push]
示例12: test_create_feed
# 需要导入模块: from google.cloud import pubsub_v1 [as 别名]
# 或者: from google.cloud.pubsub_v1 import PublisherClient [as 别名]
def test_create_feed(capsys):
client = resource_manager.Client()
project_number = client.fetch_project(PROJECT).number
full_topic_name = "projects/{}/topics/{}".format(PROJECT, TOPIC)
publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path(PROJECT, TOPIC)
publisher.create_topic(topic_path)
quickstart_createfeed.create_feed(
PROJECT, FEED_ID, [ASSET_NAME, ], full_topic_name)
out, _ = capsys.readouterr()
assert "feed" in out
# Clean up, delete the feed
feed_name = "projects/{}/feeds/{}".format(project_number, FEED_ID)
quickstart_deletefeed.delete_feed(feed_name)
publisher.delete_topic(topic_path)
示例13: test_update_feed
# 需要导入模块: from google.cloud import pubsub_v1 [as 别名]
# 或者: from google.cloud.pubsub_v1 import PublisherClient [as 别名]
def test_update_feed(capsys):
client = resource_manager.Client()
project_number = client.fetch_project(PROJECT).number
# First create the feed, which will be updated later
full_topic_name = "projects/{}/topics/{}".format(PROJECT, TOPIC)
publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path(PROJECT, TOPIC)
publisher.create_topic(topic_path)
quickstart_createfeed.create_feed(
PROJECT, FEED_ID, [ASSET_NAME, ], full_topic_name)
feed_name = "projects/{}/feeds/{}".format(project_number, FEED_ID)
new_full_topic_name = "projects/" + PROJECT + "/topics/" + NEW_TOPIC
new_topic_path = publisher.topic_path(PROJECT, NEW_TOPIC)
publisher.create_topic(new_topic_path)
quickstart_updatefeed.update_feed(feed_name, new_full_topic_name)
out, _ = capsys.readouterr()
assert "updated_feed" in out
# Clean up and delete the feed
quickstart_deletefeed.delete_feed(feed_name)
publisher.delete_topic(topic_path)
publisher.delete_topic(new_topic_path)
示例14: test_delete_feed
# 需要导入模块: from google.cloud import pubsub_v1 [as 别名]
# 或者: from google.cloud.pubsub_v1 import PublisherClient [as 别名]
def test_delete_feed(capsys):
client = resource_manager.Client()
project_number = client.fetch_project(PROJECT).number
# First create the feed, which will be deleted later
full_topic_name = "projects/{}/topics/{}".format(PROJECT, TOPIC)
publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path(PROJECT, TOPIC)
publisher.create_topic(topic_path)
quickstart_createfeed.create_feed(
PROJECT, FEED_ID, [ASSET_NAME, ], full_topic_name)
feed_name = "projects/{}/feeds/{}".format(project_number, FEED_ID)
quickstart_deletefeed.delete_feed(feed_name)
out, _ = capsys.readouterr()
assert "deleted_feed" in out
publisher.delete_topic(topic_path)
示例15: test_get_feed
# 需要导入模块: from google.cloud import pubsub_v1 [as 别名]
# 或者: from google.cloud.pubsub_v1 import PublisherClient [as 别名]
def test_get_feed(capsys):
client = resource_manager.Client()
project_number = client.fetch_project(PROJECT).number
# First create the feed, which will be gotten later
full_topic_name = "projects/{}/topics/{}".format(PROJECT, TOPIC)
publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path(PROJECT, TOPIC)
publisher.create_topic(topic_path)
quickstart_createfeed.create_feed(
PROJECT, FEED_ID, [ASSET_NAME, ], full_topic_name)
feed_name = "projects/{}/feeds/{}".format(project_number, FEED_ID)
quickstart_getfeed.get_feed(feed_name)
out, _ = capsys.readouterr()
assert "gotten_feed" in out
# Clean up and delete the feed
quickstart_deletefeed.delete_feed(feed_name)
publisher.delete_topic(topic_path)