[英]AWS IoT create things automatically
我想知道如何在不使用AWS Web界面的情况下通过API在AWS IoT解决方案中创建许多“事物”,因为如果我想要数以千计的事情,这是不现实的。 我想你可以用这里描述的“aws”客户端编写一些脚本“ http://docs.aws.amazon.com/iot/latest/developerguide/thing-registry.html ”但是如果我想控制它就不是最佳的来自其他服务。
我假设会有一个RESTish API来执行此操作但如果我阅读文档似乎不是这样: “您使用AWS IoT控制台或AWS CLI与注册表进行交互。”
任何人创造了数千/数百万的东西 - 您是如何与AWS IoT互动的?
好的,我找到了。 在这里可以管理所有AWS IoT事物:
http://docs.aws.amazon.com/iot/latest/apireference/API_Operations.html
另一种有趣的方法是使用实时注册 - 基本上你上传用于在制造时签名密钥的CA证书。
这有一些很棒的优点 -
下面的链接是创建50个Iot设备,并使用AWS Lambda和Amazon DynamoDB实现无服务器AWS IoT后端的示例。 您可以根据自己喜欢的数量改变设备; 它使用早期版本的AWS Iot Platform完成。 看一看。
这是创建50个Iot Things的代码
var AWS = require('aws-sdk');
AWS.config.region = 'ap-northeast-1';
var crypto = require('crypto');
var endpoint = "<endpoint prefix>.iot.<region>.amazonaws.com";
var iot = new AWS.Iot();
var iotdata = new AWS.IotData({endpoint: endpoint});
var topic = "registration";
var type = "MySmartIoTDevice"
//Create 50 AWS IoT Things
for(var i = 1; i < 51; i++) {
var serialNumber = "SN-"+crypto.randomBytes(Math.ceil(12/2)).toString('hex').slice(0,15).toUpperCase();
var clientId = "ID-"+crypto.randomBytes(Math.ceil(12/2)).toString('hex').slice(0,12).toUpperCase();
var activationCode = "AC-"+crypto.randomBytes(Math.ceil(20/2)).toString('hex').slice(0,20).toUpperCase();
var thing = "myThing"+i.toString();
var thingParams = {
thingName: thing
};
iot.createThing(thingParams).on('success', function(response) {
//Thing Created!
}).on('error', function(response) {
console.log(response);
}).send();
//Checking all devices were created
iot.listThings().on('success', function(response) {
var things = response.data.things;
var myThings = [];
for(var i = 0; i < things.length; i++) {
if (things[i].thingName.includes("myThing")){
myThings[i]=things[i].thingName;
}
}
if (myThings.length == 50){
console.log("myThing1 to 50 created and registered!");
}
}).on('error', function(response) {
console.log(response);
}).send();
您可以使用AWS IoT SDK来实现。 您可以在此处找到支持的AWS IoT SDK。 为此,您需要适当的凭据和策略,最简单的凭证和策略是AWS账户凭证(Access密钥以及来自IAM或Cognito用户凭证的密钥,......)。
要自动创建内容,您可以使用适用于Python的AWS IoT Device SDK,即Boto3。 您可以在此处找到有关它的更多信息。 以下代码显示了使用python和AWS凭据自动在AWS IoT中创建事物的示例:
import boto3
client = boto3.client('iot')
response = client.create_thing(
thingName=[NameOfThing],
thingTypeName=[ThingType],
attributePayload={
'attributes': {
'string': 'string'
},
'merge': True|False
},
billingGroupName='string'
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.