尝试在 Google PubSub python 中创建主题订阅时出错

     2023-02-16     37

关键词:

【中文标题】尝试在 Google PubSub python 中创建主题订阅时出错【英文标题】:Error in trying to create a topic subscription in Google PubSub python 【发布时间】:2021-03-17 01:40:59 【问题描述】:

我正在尝试在 python 中使用 Google 的 pubsub_v1 库创建对主题的订阅。我已经使用库成功创建了一个主题(创建后我可以在云控制台中看到它)。但是,我在尝试创建订阅时遇到问题。我试图解决this question 中给出的解决方案,但无济于事。这是我的订阅代码:

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 变量发生了某种变化,并且库使用了环境变量,但我仔细检查了它是正确的。我不确定我在做什么与上面提到的问题不同。谢谢。

更新

来自 cmets 的一些有用信息:

gcp-python-291817 不是项目名称 项目名称位于发布者和订阅者都从中读取的配置文件中。发布者在从文件中读取项目名称并发布消息时没有任何问题 我在此项目中有一个名为 gcp-python 的 VM 实例的 ssh 配置,但不久前被删除了 清除 gcloud 缓存和 gsutils 缓存也没有解决问题

【问题讨论】:

项目 gcp-python-291817 不存在,或者您可能没有 IAM 角色(权限)来访问它。 供参考 gcp-python-291817 不是项目名称 不管它叫什么,你都在错误地命名项目。 如果你看下面@DazWilkin 的评论,我打印出了主题和订阅的完整路径,并且正确的项目名称在字符串中。项目名称在配置文件中。发布者和订阅者文件都从这里读取,发布者没有问题 你的问题在哪里?从这些链接开始:***.com/help/minimal-reproducible-example AND ***.com/help/how-to-ask 【参考方案1】:

我发现了问题。我重新跑了

gcloud auth application-default login  

并确保 GOOGLE_APPLICATION_CREDENTIALS 指向新的凭证 json 文件并且它有效。我一定在某个时候弄乱了凭据文件。感谢您的帮助!

【讨论】:

您好!感谢您添加答案,请记住您可以将答案标记为正确。 ***.com/help/self-answer【参考方案2】:

参见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

工作样本:

创建项目,启用发布/订阅等

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)

【讨论】:

topic_path = subscriber.topic_path(project_name, topic_name) subscription_path = subscriber.subscription_path(project_name, sub_name) 就是这样做的。它是制作完整路径而不是模板字符串的“花哨”方式 吃了图书馆的一部分?如果您有信心,他们正在生成正确的完全限定名称,您应该能够按所示创建子 是的,我将它们打印出来以确保显示完整的字符串,就像你所拥有的那样 好吧...好奇心被激起。我要启动旧笔记本电脑。给我 15 我还删除了我的主目录中的 gcloud 缓存,也没有成功

pip install google-cloud-pubsub 在 docker 容器中安装失败

...tainer【发布时间】:2020-09-2707:22:36【问题描述】:我正在尝试使用pupsub模拟器。它开始了,但是当我尝试使用我的python脚本时,我收到以下错误ModuleNotFoundError:Nomodulena 查看详情

尝试在 Dataflow 中使用 Apache Beam 将数据从 Google PubSub 写入 GCS 时出错

】尝试在Dataflow中使用ApacheBeam将数据从GooglePubSub写入GCS时出错【英文标题】:GettinganErrorwhiletryingtowritedatafromGooglePubSubtoGCSusingApacheBeaminDataflow【发布时间】:2018-12-3020:06:27【问题描述】:我已经编写了以下代码来从pubsub写入流数... 查看详情

Google PubSub 如何在流式拉取消息时处理搜索到的消息

...ssages【发布时间】:2021-06-1910:13:29【问题描述】:我正在尝试在Python中处理seek(timestamp)返回的消息。我使用流式拉取订阅了非搜索消息:subscriber=pubsub_v1.SubscriberC 查看详情

Google Cloud PubSub - 列出自定义属性的更好方法?

...butes?【发布时间】:2021-08-3122:20:50【问题描述】:我正在尝试简化将数据发布到PubSub的Python代码。这有效:importosfromgoogle.cloudimportpubsub_v1importjsoncredentials_path= 查看详情

在 google pubsub 中设置 gmail 的发布权限,权限被拒绝

...denied【发布时间】:2016-09-0307:30:36【问题描述】:我正在尝试将gmail的发布权限设置为googlecloud中的pubsub主题。我实现此代码的应用程序正在AWS中运行。这是一个PHP应用程序,我使用的是2.0.0-RC7版本 查看详情

Google Pubsub Python 客户端库订阅者随机崩溃

】GooglePubsubPython客户端库订阅者随机崩溃【英文标题】:GooglePubsubPythonClientlibrarysubscribercrashesrandomly【发布时间】:2018-08-2702:30:40【问题描述】:有人可以帮助我使用GooglePubsubPython客户端库吗?我正在密切关注https://cloud.google.com/... 查看详情

GKE 上的 Google Cloud PubSub:尝试检索凭据时出现 FileNotFoundException

】GKE上的GoogleCloudPubSub:尝试检索凭据时出现FileNotFoundException【英文标题】:GoogleCloudPubSubonGKE:FileNotFoundExceptionwhileattemptingtoretrievethecredentials【发布时间】:2020-03-1816:50:50【问题描述】:我正在开发一个使用SpringGooglePubSub库(spring-... 查看详情

我无法在 Spring Boot 中使用 google pubsub 模拟器发送消息

...ngboot【发布时间】:2020-04-2212:15:36【问题描述】:我正在尝试使用pubsub的模拟器发送推送消息,我也在使用springboot,这是我的配置:依赖:<dependency><groupId>org.sp 查看详情

google cloud pubsub ImportError:无法导入名称类型

】googlecloudpubsubImportError:无法导入名称类型【英文标题】:googlecloudpubsubImportError:cannotimportnametypes【发布时间】:2018-12-1107:14:05【问题描述】:我使用google-cloud-pubsub为标准环境在python上为Googleappengine编写了一个小程序。我收到... 查看详情

将频道与 google pubsub poll 订阅者一起使用

...criber【发布时间】:2019-01-1400:05:00【问题描述】:我正在尝试在golang中创建一个googlepubsub订阅者,我一次接收100条消息,然后将它们写入influx。我正在尝试使用频道来执行此操作:packagemainimport("os""fmt""cloud.g 查看详情

在 PubSub 架构中使用 google.protobuf.Timestamp

】在PubSub架构中使用google.protobuf.Timestamp【英文标题】:Usegoogle.protobuf.TimestampinPubSubschema【发布时间】:2021-12-0715:44:12【问题描述】:我想创建一个看起来像这样的PubSub架构syntax="proto3";packageproto;import"google/protobuf/timestamp.proto";messa... 查看详情

如何在 Google.Cloud.PubSub.V1 SubscriberServiceApiClientBuilder 中配置频道选项

】如何在Google.Cloud.PubSub.V1SubscriberServiceApiClientBuilder中配置频道选项【英文标题】:HowtoconfigurechanneloptionsinGoogle.Cloud.PubSub.V1SubscriberServiceApiClientBuilder【发布时间】:2021-05-1410:06:53【问题描述】:在Google.Cloud.PubSub.V1版本1.x.x 查看详情

Google pubsub 死字在 golang 中不起作用

...orksingolang【发布时间】:2020-08-2410:42:21【问题描述】:我尝试使用googlepubsub死字。我使用控制台云为1个订阅启用死信。我已经将死信主题和maxAttemptDelivery属性设置为5。我的期望是如果1条消息在1个订阅中重新传递超过5次,则该... 查看详情

使用 Google Cloud PubSub 不断收到“向 Cloud PubSub 发送测试消息时出错...”

...PubSub【发布时间】:2015-11-1005:14:03【问题描述】:我正在尝试将Google的推送PubSub设置到我的服务器以接收Gmail推送通知。我得到以下范围:https: 查看详情

从 Google PubSub 中提取消息不起作用 - 权限被拒绝

...denied【发布时间】:2018-01-2202:40:44【问题描述】:我正在尝试从现有订阅中提取消息,但每次尝试都会收到PERMISSION_DENIED错误。但是,测试权限会为“pubsub.subscriptions.consume”返回“true”。也 查看详情

在哪里可以检查或管理使用 Google Firebase 创建的 PubSub 主题?

】在哪里可以检查或管理使用GoogleFirebase创建的PubSub主题?【英文标题】:WherecanIexamineormanagePubSubtopicscreateusingGoogleFirebase?【发布时间】:2021-12-1211:59:03【问题描述】:我使用Firebase创建了一个计划函数。部署它。我不知道在哪里... 查看详情

在 pubsub 中就地 std::move

...pubsub【发布时间】:2021-11-1117:14:31【问题描述】:在pubsubgoogle官方文档https://cloud.google.com/pubsub/docs/pull我们可以找到这样的例子。namespacepubsub=::google::cloud::pubsub;autosample=[](pubsub::Subsc 查看详情

google.cloud.pubsub_v1 和 google.cloud.pubsub 有啥区别?

】google.cloud.pubsub_v1和google.cloud.pubsub有啥区别?【英文标题】:Whatisthedifferencebetweengoogle.cloud.pubsub_v1andgoogle.cloud.pubsub?google.cloud.pubsub_v1和google.cloud.pubsub有什么区别?【发布时间】:2018-06-2005:04:42【问题描述】:我看到两者都在Goo... 查看详情