尝试在 Google PubSub python 中创建主题订阅时出错
Error in trying to create a topic subscription in Google PubSub python
我正在尝试使用 python 中的 Google 的 pubsub_v1
库创建对主题的订阅。我已经使用库成功创建了一个主题(创建后我可以在云控制台中看到它)。但是,我在尝试创建订阅时遇到问题。我尝试了 中给出的解决方案,但无济于事。这是我的订阅代码:
from google.cloud import pubsub_v1 as pubsub
topic_name = 'logs'
sub_name = 'logs-consumer'
project_name = 'my-project' # valid project name
subscriber = pubsub.SubscriberClient()
topic_path = subscriber.topic_path(project_name, topic_name)
subscription_path = subscriber.subscription_path(project_name, sub_name)
# Wrap the subscriber in a 'with' block to automatically call close() to
# close the underlying gRPC channel when done.
with subscriber:
subscription = subscriber.create_subscription(
request={"name": subscription_path, "topic": topic_path}
)
每当我 运行 这段代码时,我都会收到以下错误:
Traceback (most recent call last):
File "/Users/zacharymcgrath/Library/Python/3.7/lib/python/site-packages/google/api_core/grpc_helpers.py", line 57, in error_remapped_callable
return callable_(*args, **kwargs)
File "/Users/zacharymcgrath/Library/Python/3.7/lib/python/site-packages/grpc/_channel.py", line 826, in __call__
return _end_unary_response_blocking(state, call, False, None)
File "/Users/zacharymcgrath/Library/Python/3.7/lib/python/site-packages/grpc/_channel.py", line 729, in _end_unary_response_blocking
raise _InactiveRpcError(state)
grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
status = StatusCode.INVALID_ARGUMENT
details = "Project 'project:gcp-python-291817' not found or deleted."
debug_error_string = "{"created":"@1607133732.705528000","description":"Error received from peer ipv6:[2607:f8b0:400f:801::200a]:443","file":"src/core/lib/surface/call.cc","file_line":1062,"grpc_message":"Project 'project:gcp-python-291817' not found or deleted.","grpc_status":3}"
>
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "logger_consumer_GCP.py", line 28, in <module>
request={"name": subscription_path, "topic": topic_path}
File "/Users/zacharymcgrath/Library/Python/3.7/lib/python/site-packages/google/cloud/pubsub_v1/_gapic.py", line 40, in <lambda>
fx = lambda self, *a, **kw: wrapped_fx(self.api, *a, **kw) # noqa
File "/Users/zacharymcgrath/Library/Python/3.7/lib/python/site-packages/google/pubsub_v1/services/subscriber/client.py", line 526, in create_subscription
response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,)
File "/Users/zacharymcgrath/Library/Python/3.7/lib/python/site-packages/google/api_core/gapic_v1/method.py", line 145, in __call__
return wrapped_func(*args, **kwargs)
File "/Users/zacharymcgrath/Library/Python/3.7/lib/python/site-packages/google/api_core/retry.py", line 286, in retry_wrapped_func
on_error=on_error,
File "/Users/zacharymcgrath/Library/Python/3.7/lib/python/site-packages/google/api_core/retry.py", line 184, in retry_target
return target()
File "/Users/zacharymcgrath/Library/Python/3.7/lib/python/site-packages/google/api_core/grpc_helpers.py", line 59, in error_remapped_callable
six.raise_from(exceptions.from_grpc_error(exc), exc)
File "<string>", line 3, in raise_from
google.api_core.exceptions.InvalidArgument: 400 Project 'project:gcp-python-291817' not found or deleted.
我认为可能是我的 project
gcloud 变量发生了某种变化,库使用了环境变量,但我仔细检查了一下,结果是正确的。我不太确定我在做什么,这与上面提到的问题不同。谢谢。
更新
评论中的一些有用信息:
gcp-python-291817
不是项目名
- 项目名称在发布者和订阅者都可以读取的配置文件中。发布者从文件中读取项目名称并发布消息时没有任何问题
- 我在这个名为
gcp-python
的项目中有一个 VM 实例的 ssh
配置,但不久前被删除了
- 清除 gcloud 缓存和 gsutils 缓存也没有解决问题
参见 documentation,例如Subscribing
主题|订阅名称不是简单的名称,而是带有project/
前缀、项目ID值等
topic_name = 'projects/{project_id}/topics/{topic}'...
subscription_name = 'projects/{project_id}/subscriptions/{sub}'...
然后,按照文档中的代码:
subscriber.create_subscription(
name=subscription_name, topic=topic_name)
也许试试这个:
PROJECT=[[YOUR-PROJECT]]
gcloud projects list \
--filter=projectid=${PROJECT} \
--format="value(projectNumber)"
[[VALUE]]
PROJECT
的值与您在代码中使用的值相同。
你应该得到一个值。
那么您可以:
gcloud pubsub topics list --project=${PROJECT}
gcloud pubsub subscriptions list --project=${PROJECT}
工作样本:
创建项目,启用pub/sub等
gcloud projects create ${PROJECT}
BILLING=$(gcloud beta billing accounts list --format="value(name)")
gcloud beta billing projects link ${PROJECT} \
--billing-account=${BILLING}
gcloud services enable pubsub.googleapis.com \
--project=${PROJECT}
ROBOT="tester"
EMAIL="${ROBOT}@${PROJECT}.iam.gserviceaccount.com"
gcloud iam service-accounts create ${ROBOT} \
--project=${PROJECT}
gcloud iam service-accounts keys create ${PWD}/${ROBOT}.json \
--iam-account=${EMAIL} \
--project=${PROJECT}
gcloud projects add-iam-policy-binding ${PROJECT} \
--role=roles/pubsub.editor \
--member=serviceAccount:${EMAIL}
python3 -m venv venv
source venv/bin/activate
python3 -m pip install google-cloud-pubsub
export PROJECT
export GOOGLE_APPLICATION_CREDENTIALS=${PWD}/tester.json
python3 main.py
并且:
import os
from google.cloud import pubsub_v1
publisher = pubsub_v1.PublisherClient()
topic_name = "freddie"
topic_path = publisher.topic_path(os.getenv("PROJECT"), topic_name)
topic = publisher.create_topic(request={"name": topic_path})
publisher.publish(topic_path, b"message", spam="eggs")
subscriber = pubsub_v1.SubscriberClient()
subscription_name="cookie"
subscription_path = subscriber.subscription_path(os.getenv("PROJECT"),subscription_name)
subscription = subscriber.create_subscription(request={"name": subscription_path, "topic": topic_path})
def callback(message):
print(message.data)
message.ack()
future = subscriber.subscribe(subscription_path, callback)
我发现了问题。我重新运行
gcloud auth application-default login
并确保 GOOGLE_APPLICATION_CREDENTIALS
指向新的凭据 json 文件并且它有效。我一定是在某个时候弄乱了凭据文件。感谢您的帮助!
我正在尝试使用 python 中的 Google 的 pubsub_v1
库创建对主题的订阅。我已经使用库成功创建了一个主题(创建后我可以在云控制台中看到它)。但是,我在尝试创建订阅时遇到问题。我尝试了
from google.cloud import pubsub_v1 as pubsub
topic_name = 'logs'
sub_name = 'logs-consumer'
project_name = 'my-project' # valid project name
subscriber = pubsub.SubscriberClient()
topic_path = subscriber.topic_path(project_name, topic_name)
subscription_path = subscriber.subscription_path(project_name, sub_name)
# Wrap the subscriber in a 'with' block to automatically call close() to
# close the underlying gRPC channel when done.
with subscriber:
subscription = subscriber.create_subscription(
request={"name": subscription_path, "topic": topic_path}
)
每当我 运行 这段代码时,我都会收到以下错误:
Traceback (most recent call last):
File "/Users/zacharymcgrath/Library/Python/3.7/lib/python/site-packages/google/api_core/grpc_helpers.py", line 57, in error_remapped_callable
return callable_(*args, **kwargs)
File "/Users/zacharymcgrath/Library/Python/3.7/lib/python/site-packages/grpc/_channel.py", line 826, in __call__
return _end_unary_response_blocking(state, call, False, None)
File "/Users/zacharymcgrath/Library/Python/3.7/lib/python/site-packages/grpc/_channel.py", line 729, in _end_unary_response_blocking
raise _InactiveRpcError(state)
grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
status = StatusCode.INVALID_ARGUMENT
details = "Project 'project:gcp-python-291817' not found or deleted."
debug_error_string = "{"created":"@1607133732.705528000","description":"Error received from peer ipv6:[2607:f8b0:400f:801::200a]:443","file":"src/core/lib/surface/call.cc","file_line":1062,"grpc_message":"Project 'project:gcp-python-291817' not found or deleted.","grpc_status":3}"
>
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "logger_consumer_GCP.py", line 28, in <module>
request={"name": subscription_path, "topic": topic_path}
File "/Users/zacharymcgrath/Library/Python/3.7/lib/python/site-packages/google/cloud/pubsub_v1/_gapic.py", line 40, in <lambda>
fx = lambda self, *a, **kw: wrapped_fx(self.api, *a, **kw) # noqa
File "/Users/zacharymcgrath/Library/Python/3.7/lib/python/site-packages/google/pubsub_v1/services/subscriber/client.py", line 526, in create_subscription
response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,)
File "/Users/zacharymcgrath/Library/Python/3.7/lib/python/site-packages/google/api_core/gapic_v1/method.py", line 145, in __call__
return wrapped_func(*args, **kwargs)
File "/Users/zacharymcgrath/Library/Python/3.7/lib/python/site-packages/google/api_core/retry.py", line 286, in retry_wrapped_func
on_error=on_error,
File "/Users/zacharymcgrath/Library/Python/3.7/lib/python/site-packages/google/api_core/retry.py", line 184, in retry_target
return target()
File "/Users/zacharymcgrath/Library/Python/3.7/lib/python/site-packages/google/api_core/grpc_helpers.py", line 59, in error_remapped_callable
six.raise_from(exceptions.from_grpc_error(exc), exc)
File "<string>", line 3, in raise_from
google.api_core.exceptions.InvalidArgument: 400 Project 'project:gcp-python-291817' not found or deleted.
我认为可能是我的 project
gcloud 变量发生了某种变化,库使用了环境变量,但我仔细检查了一下,结果是正确的。我不太确定我在做什么,这与上面提到的问题不同。谢谢。
更新
评论中的一些有用信息:
gcp-python-291817
不是项目名- 项目名称在发布者和订阅者都可以读取的配置文件中。发布者从文件中读取项目名称并发布消息时没有任何问题
- 我在这个名为
gcp-python
的项目中有一个 VM 实例的ssh
配置,但不久前被删除了 - 清除 gcloud 缓存和 gsutils 缓存也没有解决问题
参见 documentation,例如Subscribing
主题|订阅名称不是简单的名称,而是带有project/
前缀、项目ID值等
topic_name = 'projects/{project_id}/topics/{topic}'...
subscription_name = 'projects/{project_id}/subscriptions/{sub}'...
然后,按照文档中的代码:
subscriber.create_subscription(
name=subscription_name, topic=topic_name)
也许试试这个:
PROJECT=[[YOUR-PROJECT]]
gcloud projects list \
--filter=projectid=${PROJECT} \
--format="value(projectNumber)"
[[VALUE]]
PROJECT
的值与您在代码中使用的值相同。
你应该得到一个值。
那么您可以:
gcloud pubsub topics list --project=${PROJECT}
gcloud pubsub subscriptions list --project=${PROJECT}
工作样本:
创建项目,启用pub/sub等
gcloud projects create ${PROJECT}
BILLING=$(gcloud beta billing accounts list --format="value(name)")
gcloud beta billing projects link ${PROJECT} \
--billing-account=${BILLING}
gcloud services enable pubsub.googleapis.com \
--project=${PROJECT}
ROBOT="tester"
EMAIL="${ROBOT}@${PROJECT}.iam.gserviceaccount.com"
gcloud iam service-accounts create ${ROBOT} \
--project=${PROJECT}
gcloud iam service-accounts keys create ${PWD}/${ROBOT}.json \
--iam-account=${EMAIL} \
--project=${PROJECT}
gcloud projects add-iam-policy-binding ${PROJECT} \
--role=roles/pubsub.editor \
--member=serviceAccount:${EMAIL}
python3 -m venv venv
source venv/bin/activate
python3 -m pip install google-cloud-pubsub
export PROJECT
export GOOGLE_APPLICATION_CREDENTIALS=${PWD}/tester.json
python3 main.py
并且:
import os
from google.cloud import pubsub_v1
publisher = pubsub_v1.PublisherClient()
topic_name = "freddie"
topic_path = publisher.topic_path(os.getenv("PROJECT"), topic_name)
topic = publisher.create_topic(request={"name": topic_path})
publisher.publish(topic_path, b"message", spam="eggs")
subscriber = pubsub_v1.SubscriberClient()
subscription_name="cookie"
subscription_path = subscriber.subscription_path(os.getenv("PROJECT"),subscription_name)
subscription = subscriber.create_subscription(request={"name": subscription_path, "topic": topic_path})
def callback(message):
print(message.data)
message.ack()
future = subscriber.subscribe(subscription_path, callback)
我发现了问题。我重新运行
gcloud auth application-default login
并确保 GOOGLE_APPLICATION_CREDENTIALS
指向新的凭据 json 文件并且它有效。我一定是在某个时候弄乱了凭据文件。感谢您的帮助!