How do I “create”/“assign” a logging handler for Google Cloud Pubsub?

后端 未结 2 702
礼貌的吻别
礼貌的吻别 2021-01-22 01:59

Development from the previous thread found that the assumptions when asking the question were off-topic (subprocess was actually not causing the problems), so I\'m making a more

2条回答
  •  星月不相逢
    2021-01-22 03:00

    As Nahuel and tripleee explained, the problem is with the messages being BYTES instead of strings. However, their code didn't exactly work, and still threw out errors, and I have no idea why. By cross-referencing with Google's sample code for the pubsub appengine website, and a few more hours of trial and error, I have found the following code to be working. Might be inelegant and/or have bad practices, in that case please edit it and make it more robust.

    #Continues from after message.ack(), above code remains unchanged
    #except needing to 
    
        #this makes a message.data a true python dict with strings.
        payload = json.loads(message.data.decode('utf-8')) 
    
        #this finds the value of the dict with key "name"
        namepath = payload["name"]
    
        #this is just a static string to pre-pend to the file path
        dirpath = "/home/[redacted]/"
    
        #combine them into a single functioning path
        fullpath = dirpath + namepath
    
        #currently type 'unicode', so convert them to type 'str'
        fullpath = fullpath.encode("utf-8")
    

    And at the end we will have a fullpath that is purely type 'str' to be used by later functions/commands.

提交回复
热议问题