如何使用登錄檔管理物件 - AWS IoT Core

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

如何使用登錄檔管理物件

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

命名您的物件物件時:
  • 不要在您的物品名稱中使用可識別個人身份的信息。物件名稱可以出現在未加密的通訊和報告中。

建立物件

下列命令顯示如何使用 CLI 中的 AWS IoT CreateThing指令來建立物件。建立物件之後,就無法變更物件的名稱。若要變更物件的名稱,請建立新物件,指定新名稱,然後刪除舊物件。

$ aws iot create-thing --thing-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

列出物件

您可以使用 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

說明物件

您可使用 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" } }

若要取得更多資訊,請參閱《指令參考》中的 AWS CLI 描述物件。

更新物件

您可以使用 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

刪除物件

您可以使用 DeleteThing 命令來刪除物件:

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

如果刪除成功或您指定的物件不存在,則此命令會成功傳回且不含錯誤。

如需詳細資訊,請參閱《 AWS CLI 命令參考》中的 delete-thing

將主體連接至物件

實體裝置必須具 AWS IoT有要與之通訊的 X.509 憑證。您可以將裝置上的憑證與登錄檔中代表您裝置的物件相關聯。若要將憑證連接至物件,請使用 AttachThingPrincipal 命令:

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

AttachThingPrincipal 命令不會產生任何輸出。

若要取得更多資訊,請參閱《指令參考》中的貼附主體。 AWS CLI

將主體與物件分離

您可以使用 DetachThingPrincipal 命令從物件分離出憑證:

$ aws iot detach-thing-principal --thing-name "MyLightBulb" --principal "arn:aws:iot:us-east-1:123456789012:cert/a0c01f5835079de0a7514643d68ef8414ab739a1e94ee4162977b02b12842847"

DetachThingPrincipal 命令不會產生任何輸出。

如需詳細資訊,請參閱從指令參考中卸離主體。 AWS CLI