简体   繁体   English

云 Function 由 Pubsub 触发

[英]Cloud Function Triggered by Pubsub

import base64
import logging

def hello_pubsub(event, context):
    """Background Cloud Function to be triggered by Pub/Sub.
    Args:
         event (dict):  The dictionary with data specific to this type of
         event. The `data` field contains the PubsubMessage message. The
         `attributes` field will contain custom attributes if there are any.
         context (google.cloud.functions.Context): The Cloud Functions event
         metadata. The `event_id` field contains the Pub/Sub message ID. The
         `timestamp` field contains the publish time.
    """
    import base64
    import time
    import json
    import requests
    print(event)

    print("""This Function was triggered by messageId {} published at {}
    """.format(context.event_id, context.timestamp))

    if 'data' in event:
        name = base64.b64decode(event['data']).decode('utf-8')
    else:
        name = 'World'
    print(name)
    print(type(name))    
    print('Hello {}!'.format(name))
    
    
    payload = json.loads(name)
    logging.debug(payload)

I was trying to execute this is cloud function but I am getting an error, which is probably because of json.loads().我试图执行这是云 function 但我收到一个错误,这可能是因为 json.loads()。 How can I get the payload as Json如何获得有效载荷为 Json

Error:错误:

line 32, in hello_pubsub payload = json.loads(name) File "/opt/python3.8/lib/python3.8/json/__init__.py", line 357, in loads return _default_decoder.decode(s) File "/opt/python3.8/lib/python3.8/json/decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/opt/python3.8/lib/python3.8/json/decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

This is working fine.这工作正常。 Please try to publish the message directly to topic on UI.请尝试将消息直接发布到 UI 上的主题。 I tried with following message and it gave the output {"abc":"123456","Def":"udebj"}我尝试使用以下消息,它给出了 output {"abc":"123456","Def":"udebj"}

在此处输入图像描述

Solved the Issue by Changing encoding.通过更改编码解决了该问题。 I was encoding string data, changed it to encode json data and it worked我正在编码字符串数据,将其更改为编码 json 数据并且它有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase HTTPS云功能触发两次 - Firebase HTTPS Cloud Function Triggered Twice Google Cloud Dataflow-Python将JSON流传输到PubSub-DirectRunner和DataflowRunner之间的区别 - Google Cloud Dataflow - Python Streaming JSON to PubSub - Differences between DirectRunner and DataflowRunner 由新 json 数据触发的 Firebase 云函数(在 url 上提供) - Firebase cloud functions triggered by new json data(served on url) 谷歌云 Function NPM - Google Cloud Function NPM 如何在HTTP触发的Azure函数中绑定模型集合? - How can I bind a collection of models in a HTTP triggered Azure Function? 在Rails 4中呈现JSON响应时未触发成功功能 - Success function not being triggered when rendering a JSON response in Rails 4 如何确定为什么Azure功能应用程序不是由Webhook触发的 - How to determine why an Azure Function App is not triggered by a webhook 将值传递给Cloud函数的正确方法 - Correct way to pass values to Cloud function 未捕获的TypeError:不是函数-JavaScript云计算 - Uncaught TypeError: not a function -JavaScript Cloud Computing 我应该使用Firebase Cloud功能吗? - should i use firebase cloud function or not?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM