我正在尝试使用 Python
使用 Google Cloud Platform AutoML
构建应用程序。我的整体代码流程如下所示:
用户交互-->数据发送到PubSub-->回调调用我的AutoML-->结果
调用 pubsub
的代码片段如下所示:
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_id
、project 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).
请帮我解决问题
最佳答案
非常感谢您的所有回复。我刚刚使用下面的代码片段示例修复了该问题
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]
原因是创建的订阅使用了拉取请求。我猜使用的回调方法概念主要用于“推送”,这可能是因为我没有提供端点和 token 来发布消息。希望我的猜测是正确的。也让我知道您的看法。
关于python - 403 权限被拒绝从 PubSub 访问 AutoML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52158771/