嗨,我在这个AWS世界里是个新手,我想做的是将python客户端连接到AWS物联网服务并发布消息,我正在使用SDK python及其示例,但我在认证过程中遇到了问题,我已经创建了东西、策略和认证,并下载了文件,但在python程序中,我不知道我是否以正确的方式写入了这些文件的路径,
首先我试着写下每个文件的整个路径,然后什么都没有;认证文件;什么都没有。弹出的错误说错误是我以前不怎么写的路径。谢谢你花时间,如果这个问题太基础了,我只是跳进去。
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
import time as t
import json
import AWSIoTPythonSDK.MQTTLib as AWSIoTPyMQTT
# Define ENDPOINT, CLIENT_ID, PATH_TO_CERT, PATH_TO_KEY, PATH_TO_ROOT, MESSAGE, TOPIC, and RANGE
ENDPOINT = "MYENDPOINT"
CLIENT_ID = "testDevice"
PATH_TO_CERT = "certificados/5a7e19a0269abe740ac8b38a1bfdab115d14074eb212167a3ba359c0d237a8c3-certificate.pem.crt"
PATH_TO_KEY = "certificados/5a7e19a0269abe740ac8b38a1bfdab115d14074eb212167a3ba359c0d237a8c3-private.pem.key"
PATH_TO_ROOT = "certificados/AmazonRootCA1.pem"
MESSAGE = "Hello World"
TOPIC = "Prueba/A"
RANGE = 20
myAWSIoTMQTTClient = AWSIoTPyMQTT.AWSIoTMQTTClient(CLIENT_ID)
myAWSIoTMQTTClient.configureEndpoint(ENDPOINT, 8883)
myAWSIoTMQTTClient.configureCredentials(PATH_TO_ROOT, PATH_TO_KEY, PATH_TO_CERT)
myAWSIoTMQTTClient.connect()
print('Begin Publish')
for i in range (RANGE):
data = "{} [{}]".format(MESSAGE, i+1)
message = {"message" : data}
myAWSIoTMQTTClient.publish(TOPIC, json.dumps(message), 1)
print("Published: '" + json.dumps(message) + "' to the topic: " + "'test/testing'")
t.sleep(0.1)
print('Publish End')
myAWSIoTMQTTClient.disconnect()
我在我的桌面上创建了一个目录来存储这些文件,它的名称是";certificados";从那里开始,我走上了这条路,但它不起作用。
OSError: certificados/AmazonRootCA1.pem: No such file or directory
此外,我正在使用VS代码来运行此应用程序。
错误很明显,它在你给它的路径上找不到CA证书文件。你给的路径将根据文件的执行位置进行解释,这很可能是相对于它本身的python文件。如果这不是桌面,那么你需要提供完全合格的路径:
因此,假设Linux,将路径更改为:
PATH_TO_CERT = "/home/user/Desktop/certificados/5a7e19a0269abe740ac8b38a1bfdab115d14074eb212167a3ba359c0d237a8c3-certificate.pem.crt"
PATH_TO_KEY = "/home/user/Desktop/certificados/5a7e19a0269abe740ac8b38a1bfdab115d14074eb212167a3ba359c0d237a8c3-private.pem.key"
PATH_TO_ROOT = "/home/user/Desktop/certificados/AmazonRootCA1.pem"