

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用登錄檔管理物件
<a name="thing-registry"></a>

您可以使用 AWS IoT 主控台、 AWS IoT API 或 AWS CLI 與登錄檔互動。下列章節說明如何利用 CLI 使用登錄檔。

**命名您的物件物件時：**
+ 請勿在物件名稱中使用個人身分識別資訊。物件名稱可以出現在未加密的通訊和報告中。

**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 Resource Name (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()`。您可以使用此函數，直接在 AWS IoT 規則內呼叫 和 `DescribeThing` `ListThingGroupsForThing` APIs，根據裝置登錄檔資料啟用即時訊息處理和路由，以動態存取和利用物件登錄檔資訊 （包括屬性、物件類型和群組成員資格）。如需詳細資訊，請參閱[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)。

如需詳細資訊，請參閱 AWS CLI 命令參考中的 [describe-thing](https://docs.aws.amazon.com//cli/latest/reference/iot/describe-thing.html)。

# 更新物件
<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。您可以透過執行 [attach-thing-principal](https://docs.aws.amazon.com//cli/latest/reference/iot/attach-thing-principal.html) 命令，將憑證或 Amazon Cognito ID 與代表您裝置的登錄檔中的物件建立關聯。

若要將憑證或 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` 欄位中指定類型。專屬附件表示您的 IoT 物件是唯一連接到憑證的物件，而且此憑證無法與任何其他物件建立關聯。非專屬附件表示您的 IoT 物件已連接至憑證，而且此憑證可以與其他物件建立關聯。如需詳細資訊，請參閱[將 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-v2 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** 命令不會產生任何輸出。

如需詳細資訊，請參閱 API *AWS IoT Core 參考*中的 [detach-thing-principal](https://docs.aws.amazon.com//iot/latest/apireference/API_DetachThingPrincipal.html)。