[英]Cloud Function to retrieve attributes from PubSub - Python
I created a GCP Cloud Functions which is triggered by a PubSub topic.我创建了一个由 PubSub 主题触发的 GCP Cloud Functions。
import base64
def hello_pubsub(event, context):
pubsub_message = base64.b64decode(event['data']).decode('utf-8')
print(pubsub_message)
I publish messages using the below command which triggers the Cloud Functions.我使用以下触发云功能的命令发布消息。
gcloud pubsub topics publish test-topic --message="test" \
--attribute="origin=gcloud-sample,username=gcp"
Using this I can access only the "message" part of the topic.使用它我只能访问主题的“消息”部分。 How can I access the "attribute" values from the PubSub message.
如何从 PubSub 消息访问“属性”值。 I want to fetch the origin and username from the topic.
我想从主题中获取来源和用户名。
From the documentation for v1 Cloud Functions : 来自 v1 Cloud Functions 的文档:
"""
event (dict): The dictionary with data specific to this type of
event. The `@type` field maps to
`type.googleapis.com/google.pubsub.v1.PubsubMessage`.
The `data` field maps to the PubsubMessage data
in a base64-encoded string. The `attributes` field maps
to the PubsubMessage attributes if any is present.
"""
So I think just using event['attributes']
should work.所以我认为只使用
event['attributes']
应该可行。 See also . 另见。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.