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
First, if I understand correctly by what you are showing in your output, you are using a Pub/Sub notification to send a message whenever you make changes to a Cloud Storage object. This information could be helpful.
Now, message.data["name"]
is not going to work because message.data is a BYTES object. Thus, can't be indexed as a dict.
To treat it as a dict, you first have to decode it as base64 (import base64
). After that, what you are left is a string which looks like JSON format. You then use json.load()
(don't forget to import json
) to transform this string into a dict. Now you can index the message.
The code for this will be:
print("This is before variables")
dirpath = "/subfolder1/"
print(dirpath)
#Transform the bytes object into a string by decoding it
namepath = base64.b64decode(message.data).decode('utf-8')
#Transform the json formated string into a dict
namepath = json.loads(namepath)
print(namepath["name"])
fullpath = dirpath + namepath["name"]
print(fullpath)
print("this is after variables")
Now, if your intent is to read the attributes only, they are properly defined at the top like:
if message.attributes:
print('Attributes:')
for key in message.attributes:
value = message.attributes.get(key)
print('{}: {}'.format(key, value))
So, you could use:
print("this is before variables")
dirpath = "~/subfolder1/"
print(dirpath)
namepath = message.attributes["objectId"]
print(namepath)
fullpath = dirpath + namepath
print(fullpath)
print("this is after variables")
Keep in mind that for this particular case, "objectId"
is the name of the file because it's the attribute that the notification from Pub/Sub for Cloud Storage uses. If you pretend to send custom messages, change "objectId"
to your desired attribute name.