[英]Cloud Function with pubsub trigger topic is not working
I have wrote a simple code to print data and context from a pubsub trigger cloud function.我编写了一个简单的代码来打印来自 pubsub 触发器云 function 的数据和上下文。
def main(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 def main(event, context): """Background Cloud Function 由 Pub/Sub 触发。args: event (dict): 包含特定于此类事件数据的字典。
data
字段包含 PubsubMessage 消息。 attributes
字段将包含自定义属性(如果有)。context (google.cloud.functions.Context):Cloud Functions 事件元数据event_id
字段包含 Pub/Sub 消息timestamp
字段包含发布时间。“””导入base64
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('Hello {}!'.format(name))
Cloud function is deployed successfully but whenever I publish a message to the trigger topic in logs I cannot see any function execution statement.云 function 部署成功,但每当我在日志中向触发器主题发布消息时,我都看不到任何 function 执行语句。
I have already verified that I am calling main function only and publishing to a right pubsub topic.我已经验证我只调用主 function 并发布到正确的 pubsub 主题。
I cannot see any error statement so I am not able to debug.我看不到任何错误语句,因此无法调试。
Any Suggestion will be helpful任何建议都会有所帮助
I tested your code function in python 3.8
runtime and all works fine, are you using the same pub/sub topic for push new messages?我在
python 3.8
运行时测试了您的代码 function 并且一切正常,您是否使用相同的发布/订阅主题来推送新消息?
This the code that I used on my computer to send pubsub messages.这是我在计算机上用来发送 pubsub 消息的代码。
from google.cloud import pubsub_v1
publisher = pubsub_v1.PublisherClient()
# The `topic_path` method creates a fully qualified identifier
# in the form `projects/{project_id}/topics/{topic_id}`
topic_path = publisher.topic_path("myprojectID", "test")
for n in range(1, 10):
data = u"Message number {}".format(n)
# Data must be a bytestring
data = data.encode("utf-8")
# When you publish a message, the client returns a future.
future = publisher.publish(topic_path, data=data)
print(future.result())
print("Published messages.")
requirements.txt要求.txt
google-cloud-pubsub
This is full function code这是完整的 function 代码
import base64
def hello_pubsub(event, context):
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('Hello {}!'.format(name))
Expected output预期 output
This Function was triggered by messageId 1449686821351887 published at 2020-08-20T21:26:30.600Z
The logs may appears with a delay of 10-30 secs on stackdriver日志可能会在堆栈驱动程序上延迟 10-30 秒出现
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.