【问题标题】:403 permission denied to access AutoML from PubSub从 PubSub 访问 AutoML 的 403 权限被拒绝
【发布时间】:2018-09-04 04:21:56
【问题描述】:

我正在尝试使用Google Cloud Platform AutoML 使用Python 构建应用程序。我的整体代码流程如下所示:

用户交互--> 发送到 PubSub 的数据--> 回调调用我的 AutoML--> 结果

调用pubsub的sn-p如下所示:

blob=blob+bytes(doc_type,'utf-8')
        publisher.publish(topic,blob)
        future=subscriber.subscribe(subscription,callback=callback)
        #flash("The object is "+future,'info')
        try:
            future.result()
        except Exception as ex:
            subscriber.close()

PubSub回调中:

def callback(message):
     new_message=message.data
     display_name,score=predict_value(new_message,"modelID","projectid",'us-central1')
     message.ack()

我的predict_value 得到model_idproject id 并计算region 并执行预测。

当我直接调用predict_value 而不使用PubSub 时,它工作正常。如果我这样做,我会收到以下错误:

google.api_core.exceptions.PermissionDenied: 403 Permission 'automl.models.predict' denied on resource 'projects/projectID/locations/us-central1/models/' (or it may not exist).

请帮我解决问题

【问题讨论】:

  • 这个回调在哪里执行?它是云功能还是具体在哪里?我的观点是执行此代码的服务帐户必须具有“automl.models.predict”权限。
  • 嗨@Temu ...谢谢您的回复。整个谷歌云平台只有一个凭证权,就是我们账户中生成的服务账户密钥。我已将其添加到顶部...全局级别
  • 我的意思是,我知道您正在尝试访问运行代码的同一个项目,对吧?

标签: python google-cloud-platform publish-subscribe google-cloud-pubsub google-cloud-automl


【解决方案1】:

非常感谢您的所有回复。我刚刚使用下面的 sn-p 示例解决了这个问题

def receive_messages_synchronously(project, subscription_name):
"""Pulling messages synchronously."""
# [START pubsub_subscriber_sync_pull]
# project           = "Your Google Cloud Project ID"
# subscription_name = "Your Pubsub subscription name"
subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(
    project, subscription_name)

# Builds a pull request with a specific number of messages to return.
# `return_immediately` is set to False so that the system waits (for a
# bounded amount of time) until at lease one message is available.
response = subscriber.pull(
    subscription_path,
    max_messages=3,
    return_immediately=False)

ack_ids = []
for received_message in response.received_messages:
    print("Received: {}".format(received_message.message.data))
    ack_ids.append(received_message.ack_id)

# Acknowledges the received messages so they will not be sent again.
subscriber.acknowledge(subscription_path, ack_ids)
# [END pubsub_subscriber_sync_pull]

创建订阅的原因是使用拉取请求。我猜使用的回调方法概念主要是用于“推送”,这可能是因为我没有给出端点和令牌来发布消息。希望我的猜测是正确的。也让我知道你的看法。

【讨论】:

  • ShriHari,你能检查一下你是否有a pubsub service account here in your console吗?看,当您调用服务(使用具有所有权限的帐户)然后该服务调用不同的服务时,中间的服务将需要使用最终服务的权限,因为使用的权限不是第一个权限,而是中间的。 A->B->C 这就是为什么 A 可以调用 C 而 B 不能。因为 A 拥有所有权限,但 B 拥有与该特定服务相关联的最少权限。
【解决方案2】:

这可能是由于以下两个因素之一:

  • 向 AutoML API 发送请求时使用的凭据无效 - pubsub 很可能在其他上下文中执行并且无法获取默认凭据

  • 模型资源名称无效(确保正确) - 应该类似于:“projects/12423534/locations/us-central1/models/23432423”

【讨论】:

  • 嗨,Michal,感谢您的回复。问题是如果我不使用 PubSub 并直接调用模型,它执行得很好。当它去 pubsub 有一个问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-10
  • 2017-04-10
  • 2015-09-07
  • 1970-01-01
  • 2014-06-03
  • 2016-04-21
  • 1970-01-01
相关资源
最近更新 更多