如何解决在云函数中消费和解码 protobuf 消息
我想使用来自主题(GCP 云发布/订阅)的发布/订阅消息,然后以某种方式加载到数据库中。
我决定创建一个云函数,其发布/订阅触发器连接到我想要从中消费消息的主题。数据以protobuf格式发送。
在编写一些 python 代码行时,我能够使用我的 .proto 文件解码消息并使用 'ParseFromString' 解析消息
import person_pb2 #This is the file generated from my .proto file with the format of the data
from google.cloud import pubsub_v1
subscriber = pubsub_v1.SubscriberClient()
def callback(message):
person = person_pb2.List()
person.ParseFromString(message.data)
print(person)
future = subscriber.subscribe(f'projects/my_project/subscriptions/test-sub',callback)
try:
future.result()
except KeyboardInterrupt:
future.cancel()
下面是我的云函数中正在执行的函数。它目前正在运行,但正如您所看到的,我没有使用从我的 .proto 文件中生成的 person_pb2。
def decode_pubsub(event,context):
import base64
print(f'message id: {context.event_id},timestamp: {context.timestamp}')
data = event['data']
decoded_data = base64.b64decode(data).decode('utf-8')
print(decoded_data)
现在问题来了:
在第一个场景中,消息的类型是
云函数内是否不需要使用.proto文件来解码消息?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。