简体   繁体   English

BASE64 在解码 pubsub 消息时在谷歌云 function 中应用时解码不起作用

[英]BASE64 decoding not working when applied in google Cloud function while decoding pubsub message

I am using below python code to encode the message in base64 format > publish it to pubsub > decode the message in base64 format in the same py file:我正在使用下面的 python 代码以 base64 格式对消息进行编码 > 将其发布到 pubsub > 在同一 py 文件中以 base64 格式解码消息:

    pubMessage = ptp_jsonObject['restaurant']
    message_bytes = str(pubMessage).encode('utf-8')
    bas64_bytes = base64.b64encode(message_bytes)
    print("PRINTING THE ENCODED MESSAGE")
    print(bas64_bytes)
    
#OUTPUT: b'eydSJzogeydyZXNfaWQnOiAnMTY1MjA0MjYnfSwgJ2lkJzogJzE2NTIwNDI2JywgJ25hbWUnOiAnQnJvdGhlciBIdWJiYXJkJywgJ2xvY2FsaXR5X3ZlcmJvc2UnOiAnTm9ydGggQ2l0eSwgRHVibGluJ30='
    
    publish_future = ptp_publisher_client.publish(ptp_topic_path, data=bas64_bytes)
    result = publish_future.result()
    print('Successfully published the event to pubsub')
    
    base64_message = base64.b64decode(bas64_bytes).decode('utf-8')
    print("PRINTING THE DECODED MESSAGE")
    print(base64_message)
    #OUTPUT: {'R': {'res_id': '16520426'}, 'id': '16520426', 'name': 'Brother Hubbard', 'locality_verbose': 'North City, Dublin'}

in the pubsub topic i can see the message content is mention as below encoded string:在 pubsub 主题中,我可以看到消息内容提到如下编码字符串:

eydSJzogeydyZXNfaWQnOiAnMTY1MjA0MjYnfSwgJ2lkJzogJzE2NTIwNDI2JywgJ25hbWUnOiAnQnJvdGhlciBIdWJiYXJkJywgJ2xvY2FsaXR5X3ZlcmJvc2UnOiAnTm9ydGggQ2l0eSwgRHVibGluJ30=

Now, I have created a cloud function which subscribe from the same topic > decode the message in base64 format and should print the decoded message as printed above, but instead it still printing the encoded string, pfb the code and result:现在,我创建了一个云 function 订阅同一主题 > 以 base64 格式解码消息,并且应该打印上面打印的解码消息,但它仍然打印编码字符串,pfb 代码和结果:

    import base64
    import json
    
    def hello_pubsub(event, context):
        pmessage = event['data']
        pubsub_message = base64.b64decode(pmessage).decode('utf-8')
        print('PRINTING DECODED MESSAGE PUBSUB')
        print(pubsub_message)
#OUTPUT: eydSJzogeydyZXNfaWQnOiAnMTY1MjA0MjYnfSwgJ2lkJzogJzE2NTIwNDI2JywgJ25hbWUnOiAnQnJvdGhlciBIdWJiYXJkJywgJ2xvY2FsaXR5X3ZlcmJvc2UnOiAnTm9ydGggQ2l0eSwgRHVibGluJ30=

Why am i not able to get the decoded string from cloud function when on the other hand the same code is working fine when used in the same py file?为什么我无法从云 function 获取解码的字符串,而另一方面,在同一个 py 文件中使用相同的代码时可以正常工作?

You're going to need some more debugging output.您将需要更多调试 output。 Clearly the event object seen in production differs from the one used by your unit test.显然,在生产中看到的event object 与单元测试使用的事件不同。 Log more details to identify the differences.记录更多详细信息以识别差异。 You should be able to "write a failing test", that is, write a test that fails in the same way the code fails in production.您应该能够“编写一个失败的测试”,即编写一个失败的测试,其方式与代码在生产中失败的方式相同。 With that in hand, you'll be in a much better position to implement a fix, which works in both environments.有了这些,您将在更好的 position 中实施修复,该修复适用于两种环境。

Verify the identical python interpreter is being used in all environments, and that its sys.path is pulling in identical versions of the imported libraries.验证在所有环境中都在使用相同的 python 解释器,并且它的sys.path正在拉入相同版本的导入库。

The client library publish method base64 encodes the message data for you, so your code is encoding the message twice but only decoding it once .客户端库发布方法 base64 为您对消息数据进行编码,因此您的代码对消息进行了两次编码,但只对其进行了一次解码。
Try changing your publish code to:尝试将您的发布代码更改为:

pubMessage = ptp_jsonObject['restaurant']
message_bytes = str(pubMessage).encode('utf-8')


publish_future = ptp_publisher_client.publish(ptp_topic_path, data=message_bytes)
result = publish_future.result()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM