继上一篇的 AWS:获取所有 IoT 事物 -> Python 获取 aws-iot 所有事物
直接上代码:
def dict_things_shadow():
session = Session(aws_access_key_id=CONST.AWS_ACCESS_KEY,
aws_secret_access_key=CONST.AWS_SECRET_KEY,
region_name=CONST.CLIENT_REGION,
)
client = session.client("iot")
things = client.list_things()
indices = client.list_indices()["indexNames"]
shadow_dict = dict()
for thing in things["things"]:
index_response = client.search_index(indexName=indices[0], queryString=thing["thingName"])
try:
shadow_dict[thing["thingName"]] = index_response["things"][0]["shadow"]
except Exception as e:
shadow_dict[thing["thingName"]] = None
return shadow_dict
if __name__ == "__main__":
dict_shadow = dict_things_shadow()
print(dict_shadow)