

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用注册表管理事物
<a name="thing-registry"></a>

您可以使用 AWS IoT 控制台、 AWS IoT API 或与注册表 AWS CLI 进行交互。以下各部分展示了如何使用 CLI 与 Registry 进行交互。

**命名事物对象时：**
+ 请勿在事物名称中使用个人身份信息。事物名称可以出现在未加密的通信和报告中。

**Topics**
+ [创建事物](create-thing.md)
+ [列出事物](list-things.md)
+ [描述事物](search-things.md)
+ [更新事物](update-thing.md)
+ [删除事物](delete-thing.md)
+ [将主体附加到事物](attach-thing-principal.md)
+ [列出与主体关联的事物](list-principal-things.md)
+ [列出与事物关联的主体](list-thing-principals.md)
+ [列出与主体 V2 关联的事物](list-principal-things-v2.md)
+ [列出与事物 V2 关联的主体](list-thing-principals-v2.md)
+ [将主体与事物分离](detach-thing-principal.md)

# 创建事物
<a name="create-thing"></a>

以下命令显示如何使用 CLI 中的 AWS IoT **CreateThing**命令来创建事物。您无法在创建事物后更改其名称。要更改事物的名称，请创建一个新事物，为其指定新名称，然后删除旧事物。

```
$ aws iot create-thing \
    --thing-type-name "MyLightBulb" \ 
    --attribute-payload "{\"attributes\": {\"wattage\":\"75\", \"model\":\"123\"}}"
```

**CreateThing** 命令显示新事物的名称和 Amazon 资源名称（ARN）：

```
{
    "thingArn": "arn:aws:iot:us-east-1:123456789012:thing/MyLightBulb",
    "thingName": "MyLightBulb",
    "thingId": "12345678abcdefgh12345678ijklmnop12345678"
}
```

**注意**  
我们建议不要在您的事物名称中使用个人身份信息。

有关更多信息，请参阅《 AWS CLI 命令参考》中的 [create-thing](https://docs.aws.amazon.com//cli/latest/reference/iot/create-thing.html)。

# 列出事物
<a name="list-things"></a>

您可以使用 **ListThings** 命令列出您的账户中的所有事物：

```
$ aws iot list-things
```

```
{
    "things": [
       {
            "attributes": {
                "model": "123",
                "wattage": "75"
            },
            "version": 1,
            "thingName": "MyLightBulb"
        },
        {
            "attributes": {
                "numOfStates":"3"
             },
            "version": 11,
            "thingName": "MyWallSwitch"
        }
    ]
}
```

您可以使用 **ListThings** 命令搜索某个事物类型的所有事物：

```
$  aws iot list-things --thing-type-name "LightBulb"
```

```
{
    "things": [
        {
            "thingTypeName": "LightBulb",
            "attributes": {
                "model": "123",
                "wattage": "75"
            },
            "version": 1,
            "thingName": "MyRGBLight"
        },
        {
            "thingTypeName": "LightBulb",
            "attributes": {
                "model": "123",
                "wattage": "75"
            },
            "version": 1,
            "thingName": "MySecondLightBulb"
        }
    ]
}
```

您可以使用 **ListThings** 命令搜索某个属性为特定值的所有事物。此命令最多可搜索三个属性。

```
$  aws iot list-things --attribute-name "wattage" --attribute-value "75"
```

```
{
    "things": [
        {
            "thingTypeName": "StopLight",
            "attributes": {
                "model": "123",
                "wattage": "75"
            },
            "version": 3,
            "thingName": "MyLightBulb"
        },
        {
            "thingTypeName": "LightBulb",
            "attributes": {
                "model": "123",
                "wattage": "75"
            },
            "version": 1,
            "thingName": "MyRGBLight"
        },
        {
            "thingTypeName": "LightBulb",
            "attributes": {
                "model": "123",
                "wattage": "75"
            },
            "version": 1,
            "thingName": "MySecondLightBulb"
        }
    ]
}
```

有关更多信息，请参阅《 AWS CLI 命令参考》中的 [list-things](https://docs.aws.amazon.com//cli/latest/reference/iot/list-things.html)。

# 描述事物
<a name="search-things"></a>

您可以使用 **DescribeThing** 命令获取某事物的更多详情：

```
$ aws iot describe-thing --thing-name "MyLightBulb"
{
    "version": 3,
    "thingName": "MyLightBulb",
    "thingArn": "arn:aws:iot:us-east-1:123456789012:thing/MyLightBulb",
    "thingId": "12345678abcdefgh12345678ijklmnop12345678",
    "defaultClientId": "MyLightBulb",
    "thingTypeName": "StopLight",
    "attributes": {
        "model": "123",
        "wattage": "75"
    }
}
```

您也可以使用内联函数在规则引擎中访问此 API `get_registry_data()`。您可以使用此功能动态访问和利用事物注册表信息（包括属性、事物类型和群组成员资格），方法是`ListThingGroupsForThing` APIs 直接在 AWS IoT 规则内调用`DescribeThing`，从而根据您的设备注册表数据进行实时消息处理和路由。有关更多信息，请参阅 [https://docs.aws.amazon.com//iot/latest/developerguide/iot-sql-functions.html#iot-sql-function-get-registry_data](https://docs.aws.amazon.com//iot/latest/developerguide/iot-sql-functions.html#iot-sql-function-get-registry_data)。

有关更多信息，请参阅《命令参考》中的 [describe-](https://docs.aws.amazon.com//cli/latest/reference/iot/describe-thing.html) thing AWS CLI 。

# 更新事物
<a name="update-thing"></a>

您可以使用 **UpdateThing** 命令更新事物。此命令仅更新事物的属性。您无法更改事物的名称。要更改事物的名称，请创建一个新事物，为其指定新名称，然后删除旧事物。

```
$ aws iot update-thing --thing-name "MyLightBulb" --attribute-payload "{\"attributes\": {\"wattage\":\"150\", \"model\":\"456\"}}"
```

**UpdateThing** 命令不会生成任何输出。您可以使用 **DescribeThing** 命令查看结果：

```
$ aws iot describe-thing --thing-name "MyLightBulb"
{
    "attributes": {
        "model": "456",
        "wattage": "150"
    },
    "version": 2,
    "thingName": "MyLightBulb"
}
```

有关更多信息，请参阅《 AWS CLI 命令参考》中的 [update-thing](https://docs.aws.amazon.com//cli/latest/reference/iot/update-thing.html)。

# 删除事物
<a name="delete-thing"></a>

您可以使用 **DeleteThing** 命令删除事物：

```
$ aws iot delete-thing --thing-name "MyThing"
```

如果删除成功或者您指定的事物不存在，此命令将成功返回且没有错误。

有关更多信息，请参阅《 AWS CLI 命令参考》中的 [delete-thing](https://docs.aws.amazon.com//cli/latest/reference/iot/delete-thing.html)。

# 将主体附加到事物
<a name="attach-thing-principal"></a>

物理设备可以使用主设备与之通信 AWS IoT。主体可以是 X.509 证书或 Amazon Cognito ID。通过运行命令，您可以将证书或 Amazon Cognito ID 与注册表中代表您设备的内容相关联。[attach-thing-principal](https://docs.aws.amazon.com//cli/latest/reference/iot/attach-thing-principal.html)

要将证书或 Amazon Cognito ID 附加到您的东西上，请使用以下[attach-thing-principal](https://docs.aws.amazon.com//cli/latest/reference/iot/attach-thing-principal.html)命令：

```
$ aws iot attach-thing-principal \
    --thing-name "MyLightBulb1" \
    --principal "arn:aws:iot:us-east-1:123456789012:cert/a0c01f5835079de0a7514643d68ef8414ab739a1e94ee4162977b02b12842847"
```

要使用附件类型（独占附件或非独占附件）将证书附加到您的事物，请使用[https://awscli.amazonaws.com/v2/documentation/api/latest/reference/iot/attach-thing-principal.html](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/iot/attach-thing-principal.html)命令并在`--thing-principal-type`字段中指定类型。独占附件意味着您的物联网事物是唯一附加到该证书的事物，并且该证书不能与任何其他事物关联。非独占附件意味着您的物联网事物附加到该证书，并且该证书可以与其他事物关联。有关更多信息，请参阅 [将 AWS IoT 事物关联到 MQTT 客户端连接](exclusive-thing.md)。

**注意**  
对于 [将 AWS IoT 事物关联到 MQTT 客户端连接](exclusive-thing.md) 功能，您只能使用 X.509 证书作为主体。

```
$ aws iot attach-thing-principal \
    --thing-name "MyLightBulb2" \
    --principal "arn:aws:iot:us-east-1:123456789012:cert/a0c01f5835079de0a7514643d68ef8414ab739a1e94ee4162977b02b12842847" \
    --thing-principal-type "EXCLUSIVE_THING"
```

如果附件成功，**AttachThingPrincipal** 命令不会产生任何输出。要描述附件，请使用 list-thing-principals-v 2 CLI 命令。

有关更多信息，请参阅《AWS IoT Core API 参考》**中的 [AttachThingPrincipal](https://docs.aws.amazon.com//iot/latest/apireference/API_AttachThingPrincipal.html)。

# 列出与主体关联的事物
<a name="list-principal-things"></a>

要列出与指定主体关联的事物，请运行 [https://awscli.amazonaws.com/v2/documentation/api/latest/reference/iot/list-principal-things.htmls](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/iot/list-principal-things.htmls) 命令。请注意，此命令不会列出事物和证书之间的附件类型。要列出附件类型，请使用 [https://awscli.amazonaws.com/v2/documentation/api/latest/reference/iot/list-principal-thingsv2.html](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/iot/list-principal-thingsv2.html) 命令。有关更多信息，请参阅 [列出与主体 V2 关联的事物](list-principal-things-v2.md)。

```
$ aws iot list-principal-things \
    --principal "arn:aws:iot:us-east-1:123456789012:cert/2e1eb273792174ec2b9bf4e9b37e6c6c692345499506002a35159767055278e8"
```

输出类似于以下内容：

```
{
    "things": [
        "MyLightBulb1",
        "MyLightBulb2"
    ]
}
```

有关更多信息，请参阅《AWS IoT Core API 参考》**中的 [ListPrincipalThings](https://docs.aws.amazon.com//iot/latest/apireference/API_ListPrincipalThings.html)。

# 列出与事物关联的主体
<a name="list-thing-principals"></a>

要列出与指定事物关联的主体，请运行 [https://awscli.amazonaws.com/v2/documentation/api/latest/reference/iot/list-thing-principals.html](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/iot/list-thing-principals.html) 命令。请注意，此命令不会列出事物和证书之间的附件类型。要列出附件类型，请使用 [https://awscli.amazonaws.com/v2/documentation/api/latest/reference/iot/list-thing-principalsv2.html](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/iot/list-thing-principalsv2.html) 命令。有关更多信息，请参阅 [列出与事物 V2 关联的主体](list-thing-principals-v2.md)。

```
$ aws iot list-thing-principals \
    --thing-name "MyLightBulb1"
```

输出类似于以下内容：

```
{
    "principals": [
         "arn:aws:iot:us-east-1:123456789012:cert/2e1eb273792174ec2b9bf4e9b37e6c6c692345499506002a35159767055278e8",
         "arn:aws:iot:us-east-1:123456789012:cert/1a234b39b4b68278f2e9d84bf97eac2cbf4a1c28b23ea29a44559b9bcf8d395b"
    ]
}
```

有关更多信息，请参阅《AWS IoT Core API 参考》**中的 [ListThingPrincipals](https://docs.aws.amazon.com//iot/latest/apireference/API_ListThingPrincipals.html)。

# 列出与主体 V2 关联的事物
<a name="list-principal-things-v2"></a>

要列出与指定证书关联的事物以及附件类型，请运行 [https://awscli.amazonaws.com/v2/documentation/api/latest/reference/iot/list-principal-thingsv2.html](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/iot/list-principal-thingsv2.html) 命令。附件类型指的是证书如何附加到事物。

```
$ aws iot list-principal-things-v2 \
    --principal "arn:aws:iot:us-east-1:123456789012:cert/2e1eb273792174ec2b9bf4e9b37e6c6c692345499506002a35159767055278e8"
```

输出类似于以下内容：

```
{
    "PrincipalThingObjects": [
        {
            "thingPrincipalType": "NON_EXCLUSIVE_THING",
            "thing": "arn:aws:iot:us-east-1:123456789012:thing/thing_1"
        }, 
        {
            "thingPrincipalType": "NON_EXCLUSIVE_THING",
            "thing": "arn:aws:iot:us-east-1:123456789012:thing/thing_2"
        }

    ]
}
```

有关更多信息，请参阅 *AWS IoT Core API 参考*中的 [ListPrincipalThingsV2](https://docs.aws.amazon.com//iot/latest/apireference/API_ListPrincipalThingsV2.html)。

# 列出与事物 V2 关联的主体
<a name="list-thing-principals-v2"></a>

要列出与指定事物关联的证书以及附件类型，请运行 [https://awscli.amazonaws.com/v2/documentation/api/latest/reference/iot/list-thing-principalsv2.html](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/iot/list-thing-principalsv2.html) 命令。附件类型指的是证书如何附加到事物。

```
$ aws iot list-thing-principals-v2 \
    --thing-name "thing_1"
```

输出类似于以下内容：

```
{
    "ThingPrincipalObjects": [
        {
            "thingPrincipalType": "NON_EXCLUSIVE_THING",
            "principal": "arn:aws:iot:us-east-1:123456789012:cert/2e1eb273792174ec2b9bf4e9b37e6c6c692345499506002a35159767055278e8"
        },
        {
            "thingPrincipalType": "NON_EXCLUSIVE_THING",
            "principal": "arn:aws:iot:us-east-1:123456789012:cert/1a234b39b4b68278f2e9d84bf97eac2cbf4a1c28b23ea29a44559b9bcf8d395b"
        }
    ]
}
```

有关更多信息，请参阅 *AWS IoT Core API 参考*中的 [ListThingsPrincipalV2](https://docs.aws.amazon.com//iot/latest/apireference/API_ListThingPrincipalsV2.html)。

# 将主体与事物分离
<a name="detach-thing-principal"></a>

您可以使用 `DetachThingPrincipal` 命令将证书与事物分离：

```
$ aws iot detach-thing-principal \
    --thing-name "MyLightBulb" \
    --principal "arn:aws:iot:us-east-1:123456789012:cert/2e1eb273792174ec2b9bf4e9b37e6c6c692345499506002a35159767055278e8"
```

**DetachThingPrincipal** 命令不会生成任何输出。

有关更多信息，请参阅《AWS IoT Core API 参考》**中的 [detach-thing-principal](https://docs.aws.amazon.com//iot/latest/apireference/API_DetachThingPrincipal.html)。