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:
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:
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:
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 ?
CodePudding user response:
You're going to need some more debugging output.
Clearly the event
object seen in production
differs from the one used by your unit test.
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.
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.
CodePudding user response:
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.
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()