AWS物联网(IoT)仅在示例主题上运行MQTT吗?

huangapple go评论74阅读模式
英文:

AWS IoT MQTT only works on the example topic?

问题

以下是翻译好的内容:

我刚刚开始在AWS和物联网方面进行了一些尝试。通过使用文档和教程,我成功地创建了一个可以工作的发布应用程序,从示例类中提取出来的:

public static void main(String[] args) throws AWSIotException, InterruptedException {
    String clientEndpoint = "<prefix>-ats.iot.us-west-2.amazonaws.com";   // 使用您自己的<prefix>和<region>进行替换
    String clientId = "sdk-java-23";                                      // 使用您自己的客户端ID进行替换。对于并发连接,请使用唯一的客户端ID。
    String certificateFile = "athing.cert.pem";                            // 基于X.509的证书文件
    String privateKeyFile = "athing.private.key";                          // PKCS#1或PKCS#8编码的私钥文件

    // SampleUtil.java及其依赖的PrivateKeyReader.java可以从示例源代码中复制。
    // 或者,您可以直接从文件加载密钥库 - 参见此自述文件中的示例。

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

    // 在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("消息已发送");
        Thread.sleep(2000);
    }
}

我可以在AWS控制台上成功看到消息的传递:

但是,如果我仅仅将发布主题从:

String topic = "sdk/test/java";

更改为:

String topic = "sensors/temperature";

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

2020年4月4日 下午4:29:05 com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionSuccess
INFO: 连接成功建立
2020年4月4日 下午4:29:05 com.amazonaws.services.iot.client.core.AbstractAwsIotClient onConnectionSuccess
INFO: 客户端连接已激活:sdk-java
2020年4月4日 下午4:29:05 com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionFailure
INFO: 连接暂时丢失
2020年4月4日 下午4:29:05 com.amazonaws.services.iot.client.core.AbstractAwsIotClient onConnectionFailure
INFO: 客户端连接丢失:sdk-java
2020年4月4日 下午4:29:08 com.amazonaws.services.iot.client.core.AwsIotConnection$1 run
INFO: 正在尝试重新连接
2020年4月4日 下午4:29:11 com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionSuccess
INFO: 连接成功建立
2020年4月4日 下午4:29:11 com.amazonaws.services.iot.client.core.AbstractAwsIotClient onConnectionSuccess
INFO: 客户端连接已激活:sdk-java

英文:

I've just gotten started in AWS and IoT. Using the documentation and the tutorial I managed to get a working publish app ripped from the sample classes:

    public static void main(String[] args) throws AWSIotException, InterruptedException {
String clientEndpoint = &quot;&lt;prefix&gt;-ats.iot.us-west-2.amazonaws.com&quot;;       // replace &lt;prefix&gt; and &lt;region&gt; with your own
String clientId = &quot;sdk-java-23&quot;;                              // replace with your own client ID. Use unique client IDs for concurrent connections.
String certificateFile = &quot;athing.cert.pem&quot;;                       // X.509 based certificate file
String privateKeyFile = &quot;athing.private.key&quot;;                        // 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 = &quot;sdk/test/java&quot;;
String payload = &quot;[\n&quot; +
&quot;{\n&quot; +
&quot; \&quot;id\&quot;: \&quot;1231231234123\&quot;,\n&quot; +
&quot; \&quot;value\&quot;: \&quot;25\&quot;,\n&quot; +
&quot; \&quot;unit\&quot;: \&quot;&#176;C\&quot;,\n&quot; +
&quot; \&quot;timestamp\&quot;: \&quot;1585954728\&quot;\n&quot; +
&quot;},\n&quot; +
&quot;{\n&quot; +
&quot;  \&quot;id\&quot;: \&quot;121231231233\&quot;,\n&quot; +
&quot;  \&quot;value\&quot;: \&quot;26\&quot;,\n&quot; +
&quot;  \&quot;unit\&quot;: \&quot;&#176;B\&quot;,\n&quot; +
&quot;  \&quot;timestamp\&quot;: \&quot;1585254728\&quot;\n&quot; +
&quot;}&quot;+
&quot;]&quot;;
System.out.println(payload);
while (true) {
client.publish(topic, AWSIotQos.QOS0, payload);
System.out.println(&quot;message sent&quot;);
Thread.sleep(2000);
}
}

And I can see the messages coming through successfully on the aws console:

AWS物联网(IoT)仅在示例主题上运行MQTT吗?

But if I change JUST the publish topic from:

String topic = &quot;sdk/test/java&quot;;

to:

String topic = &quot;sensors/temperature&quot;;

Now it no longer works. I don't see anything appear in the AWS console and the java program is showing some kind of connection error. My first instinct is some kind of security issue where it's not allowed to publish to any topic other than the one that is used in the sample program. I have no experience with IAM, cognito etc. so I would require some guidance (if that is the cause)

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

答案1

得分: 1

所以事实证明这只是一个策略问题,我完全不知道你必须要定义哪些ClientIDs和哪些主题被允许发布/订阅等。

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

So it turns out it was just a policy issue, I had no idea you had to define which ClientIDs and which topics are allowed to published/subscribed etc.

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

huangapple
  • 本文由 发表于 2020年4月4日 22:30:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/61029613.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定