我正在将图像从树莓Pi发送到云物联网核心。这是可行的,但我现在意识到,在谷歌云存储中存储文件时,我还需要文件名(或我添加到文件名中的数据),但据我所知,发送的只是文件的内容。我试图用下面的代码提取来完成这一点,但这似乎不起作用,我想这是可以理解的,因为它是JSON对象中的字节数组
有办法吗?当通过云函数发送到存储器时,文件名是否可以检索
with open(filename, 'rb') as f:
imagestring = f.read()
byteArray = bytes(imagestring)
now = datetime.datetime.now()
datetime_formatted = now.strftime("%Y-%m-%d_%H:%M:%S")
filename = 'img_GM4_' + datetime_formatted +'.jpg'
payload = '{{ "ts": {}, filename: {}, "image": {}}}'.format(int(time.time()), filename, byteArray)
client.publish(_MQTT_TOPIC, payload, qos=1)
# Previously: client.publish(_MQTT_TOPIC, byteArray, qos=1)
为了在云存储中保存映像,您需要使用Cloud Function with a Cloud Pub/Sub trigger
云功能应包括以下步骤:
最后,请注意,根据best practices for Cloud Storage,您应该避免文件名基于时间戳(这将限制吞吐量)。在云存储环境中,建议在following manner中创建文件名。因此,如果您注意到应用程序中的性能问题,请考虑最后一条消息
相关问题 更多 >
编程相关推荐