java实现 AWS IOT 创建事物、创建证书、 证书和事物关联 CertificateAttachThing

java实现 AWS IOT 创建事物、创建证书、关联Thing和Certificate。

友情提示:你要官方文档不?只要你开金口,我立马给你送过来。疯狂点击aws iot官方文档

哇哈哈哈哈哈哈哈,不bb了进入正题。

一、 创建事物
        CreateThingRequest thingRequest = new CreateThingRequest();
        thingRequest.setThingName("XXXXXX");
        thingRequest.setThingTypeName("CPE");
        CreateThingResult thingResult = iotClient.createThing(thingRequest);
        if (null == thingResult) {
            System.out.println("createThing 进行iot创建事物未收到返回结果");
            throw new RuntimeException("createThing failed");
        }
二、 创建证书
        CreateKeysAndCertificateRequest createKeysAndCertificateRequest = new CreateKeysAndCertificateRequest();
        createKeysAndCertificateRequest.setSetAsActive(false);
        CreateKeysAndCertificateResult certificateResult = iotClient.createKeysAndCertificate(createKeysAndCertificateRequest);
         if (null == certificateResult) {
            System.out.println("createCertificate 进行iot创建证书未收到返回结果");
            throw new RuntimeException("createCertificate failed");
        } 
三、 关联事物和证书

thingName是创thing时指定的,certificateArn是创建证书返回的,标识唯一。

注意:证书和事物均已存在方可关联。

    /**
     * certificateAttachThing
     *
     * @param certificateArn
     * @param thingName
     * @return
     */
    public boolean certificateAttachThing(String certificateArn, String thingName) {
        try {
            //获取iot服务客户端
            if (null == iotClient) {
                log.error("certificateAttachThing 获取iot服务出现异常");
                return false;
            }
            // 附加事物
            // thingName是创thing时指定的。
            // certificateArn是创建证书返回的,标识唯一。
            AttachThingPrincipalRequest thingPrincipalRequest = new AttachThingPrincipalRequest();
            thingPrincipalRequest.setThingName(thingName);
            thingPrincipalRequest.setPrincipal(certificateArn);
            AttachThingPrincipalResult thingPrincipalResult = iotClient.attachThingPrincipal(thingPrincipalRequest);
            if (null == thingPrincipalResult) {
                log.error("certificateAttachThing 进行证书附加事物未收到返回结果");
                return false;
            }
            //附加成功
            return true;
        } catch (Exception e) {
            log.error("certificateAttachThing", e);
        }
        return false;
    }
如何查看是否关联成功

1、在这里可以看到你所有Thing
在这里插入图片描述
2、点击一个Thing进入,会看到以下页面。
在这里插入图片描述
3、关联成功
在这里插入图片描述
该Thing关联证书的详情
在这里插入图片描述
Thing和Certificate未关联的是以下图片
在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章