【问题标题】:AWS IoT MQTT only works on the example topic?AWS IoT MQTT 仅适用于示例主题?
【发布时间】:2020-04-04 22:50:58
【问题描述】:

我刚刚开始使用 AWS 和 IoT。使用文档和教程,我设法从示例类中获取了一个工作发布应用程序:

    public static void main(String[] args) throws AWSIotException, InterruptedException {
        String clientEndpoint = "<prefix>-ats.iot.us-west-2.amazonaws.com";       // replace <prefix> and <region> with your own
        String clientId = "sdk-java-23";                              // replace with your own client ID. Use unique client IDs for concurrent connections.
        String certificateFile = "athing.cert.pem";                       // X.509 based certificate file
        String privateKeyFile = "athing.private.key";                        // PKCS#1 or PKCS#8 PEM encoded private key file

// SampleUtil.java and its dependency PrivateKeyReader.java can be copied from the sample source code.
// Alternatively, you could load key store directly from a file - see the example included in this README.

        SampleUtil.KeyStorePasswordPair pair = SampleUtil.getKeyStorePasswordPair(certificateFile, privateKeyFile);
        AWSIotMqttClient client = new AWSIotMqttClient(clientEndpoint, clientId, pair.keyStore, pair.keyPassword);

// optional parameters can be set before connect()
        client.connect();

        String topic = "sdk/test/java";
        String payload = "[\n" +
                "{\n" +
                " \"id\": \"1231231234123\",\n" +
                " \"value\": \"25\",\n" +
                " \"unit\": \"°C\",\n" +
                " \"timestamp\": \"1585954728\"\n" +
                "},\n" +
                "{\n" +
                "  \"id\": \"121231231233\",\n" +
                "  \"value\": \"26\",\n" +
                "  \"unit\": \"°B\",\n" +
                "  \"timestamp\": \"1585254728\"\n" +
                "}"+
                "]";

        System.out.println(payload);
        while (true) {
            client.publish(topic, AWSIotQos.QOS0, payload);
            System.out.println("message sent");
            Thread.sleep(2000);
        }

    }

我可以在 aws 控制台上看到成功通过的消息:

但如果我将发布主题更改为:

String topic = "sdk/test/java";

到:

String topic = "sensors/temperature";

现在它不再起作用了。我没有看到 AWS 控制台中出现任何内容,并且 java 程序显示某种连接错误。我的第一直觉是某种安全问题,不允许发布到示例程序中使用的主题以外的任何主题。我没有 IAM、cognito 等方面的经验,所以我需要一些指导(如果这是原因的话)

Apr 04, 2020 4:29:05 PM com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionSuccess
INFO: Connection successfully established
Apr 04, 2020 4:29:05 PM com.amazonaws.services.iot.client.core.AbstractAwsIotClient onConnectionSuccess
INFO: Client connection active: sdk-java
Apr 04, 2020 4:29:05 PM com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionFailure
INFO: Connection temporarily lost
Apr 04, 2020 4:29:05 PM com.amazonaws.services.iot.client.core.AbstractAwsIotClient onConnectionFailure
INFO: Client connection lost: sdk-java
Apr 04, 2020 4:29:08 PM com.amazonaws.services.iot.client.core.AwsIotConnection$1 run
INFO: Connection is being retried
Apr 04, 2020 4:29:11 PM com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionSuccess
INFO: Connection successfully established
Apr 04, 2020 4:29:11 PM com.amazonaws.services.iot.client.core.AbstractAwsIotClient onConnectionSuccess
INFO: Client connection active: sdk-java

【问题讨论】:

    标签: java amazon-web-services mqtt aws-iot


    【解决方案1】:

    所以事实证明这只是一个政策问题,我不知道您必须定义哪些 ClientID 以及允许发布/订阅哪些主题等。

    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": [
            "iot:Publish",
            "iot:Receive"
          ],
          "Resource": [
            "arn:aws:iot:us-west-2:<>:topic/sensors/realtime",
            "arn:aws:iot:us-west-2:<>:topic/sdk/test/java",
            "arn:aws:iot:us-west-2:<>:topic/sdk/test/Python",
            "arn:aws:iot:us-west-2:<>:topic/topic_1",
            "arn:aws:iot:us-west-2:<>:topic/topic_2"
          ]
        },
        {
          "Effect": "Allow",
          "Action": [
            "iot:Subscribe"
          ],
          "Resource": [
            "arn:aws:iot:us-west-2:<>:topicfilter/sensors/realtime",
            "arn:aws:iot:us-west-2:<>:topicfilter/sdk/test/java",
            "arn:aws:iot:us-west-2:<>:topicfilter/sdk/test/Python",
            "arn:aws:iot:us-west-2:<>:topicfilter/topic_1",
            "arn:aws:iot:us-west-2:<>:topicfilter/topic_2"
          ]
        },
        {
          "Effect": "Allow",
          "Action": [
            "iot:Connect"
          ],
          "Resource": [
            "arn:aws:iot:us-west-2:<>:client/JavaClient2",
            "arn:aws:iot:us-west-2:<>:client/sdk-java",
            "arn:aws:iot:us-west-2:<>:client/basicPubSub",
            "arn:aws:iot:us-west-2:<>:client/sdk-nodejs-*"
          ]
        }
      ]
    }
    

    【讨论】:

    • 感谢您分享您的问题和解决方案。您是否尝试过从您的设备发送消息并在您的 Java 应用程序中接收它?来自不同设备的特定主题。
    猜你喜欢
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 2021-03-24
    • 2017-02-22
    • 2016-12-22
    • 2016-05-22
    • 2018-12-03
    • 2018-05-01
    相关资源
    最近更新 更多