简体   繁体   English

Cloud Function 从 PubSub 检索属性 - Python

[英]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.

相关问题 Google Cloud PubSub - 如何将多个 arguments 发送到云端 Function - Google Cloud PubSub - How to send multiple arguments to the Cloud Function 如何在没有云的情况下使用 Google PubSub Function - How to use Google PubSub without Cloud Function pubsub 从一个 Function 后台发布? - Pubsub from a Function published in the background? 在 Python/Django 中从 Google Cloud Storage/ Buckets 上传和检索文件 - Upload and retrieve files from Google Cloud Storage/ Buckets in Python/Django 如何使用 terraform 使用消息排序制作 PubSub 触发的 Cloud Function? - How to make a PubSub triggered Cloud Function with message ordering using terraform? Google Cloud Function 不在 PubSub 上发布,超过超时 - Google Cloud Function don't publish on PubSub, Timeout exceeded 超出速率限制:将数据从 pubsub 主题上传到 BQ 时,云 Function 中此表的表更新操作过多 - Exceeded rate limits: too many table update operations for this table in Cloud Function while uploading the data from pubsub topic to BQ 使用 API 密钥从外部 IP 地址检索 PubSub Schema - Retrieve PubSub Schema from external IP address with API Key 如何使用消息排序部署 pubsub 触发的云 function? - How to deploy pubsub-triggered cloud function with message ordering? 从 Memorystore Redis PubSub 消息触发 Cloud Run API - Triggering Cloud Run API from a Memorystore Redis PubSub Message
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM