Python程序,用于自动创建aws物联网控制台界面所需的内容、策略和证书

Python程序,用于自动创建aws物联网控制台界面所需的内容、策略和证书,python,aws-lambda,boto3,Python,Aws Lambda,Boto3,我使用aws-iot-sdk-node.js进行编程生成。 [ 但我希望python程序通过生成内容、策略和证书来自动注册raspberry pi,并下载证书以供进一步使用 var AWS = require('aws-sdk'); AWS.config.region = 'region'; AWS.config.update({ accessKeyId: "your Key", secretAccessKey: "your Key", }); var iot = new AW

我使用aws-iot-sdk-node.js进行编程生成。 [ 但我希望python程序通过生成内容、策略和证书来自动注册raspberry pi,并下载证书以供进一步使用

var AWS = require('aws-sdk');
AWS.config.region = 'region';
AWS.config.update({
    accessKeyId: "your Key",
    secretAccessKey: "your Key",
});

var iot = new AWS.Iot();
var crypto = require('crypto');
var endpoint = "your endpoint"
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();

  //Publish JSON to Registration Topic

  var registrationData = '{\n \"serialNumber\": \"'+serialNumber+'\",\n \"clientId\": \"'+clientId+'\",\n \"device\": \"'+thing+'\",\n \"endpoint\": \"'+endpoint+'\",\n\"type\": \"'+type+'\",\n \"activationCode\": \"'+activationCode+'\",\n \"activated\": \"false\",\n \"email\": \"not@registered.yet\" \n}';

  var registrationParams = {
    topic: topic,
    payload: registrationData,
    qos: 0
  };

  iotdata.publish(registrationParams, function(err, data) {
    if (err) console.log(err, err.stack); // an error occurred
    // else Published Successfully!
  });
  setTimeout(function(){},50);
}

//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();
var AWS=require('AWS-sdk');
AWS.config.region='region';
AWS.config.update({
accessKeyId:“您的密钥”,
secretAccessKey:“您的密钥”,
});
var iot=新的AWS.iot();
var crypto=require('crypto');
var endpoint=“您的端点”
var iotdata=new AWS.iotdata({endpoint:endpoint});
var topic=“注册”;
var type=“MySmartIoTDevice”
//创建50个AWS物联网
对于(变量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();
变量thingParams={
事物名称:事物
};
iot.createThing(thingParams).on('success',函数(响应){
//创造的东西!
}).on('error',函数(响应){
控制台日志(响应);
}).send();
//将JSON发布到注册主题
var registrationData='{\n\'serialNumber\':\“+serialNumber+'”,\n\'clientId\':“'+clientId+'”,\n\'device\':“+thing+'”,\n\'endpoint\':“'+endpoint+'”,\n\'type\':“'+type+'”,\n\'activationCode\':“:”,\n\'activationCode+',\n\'activationCode+',\n\'false\',\n\'not@registered.yet\“\n}”;
变量注册参数={
主题:主题,,
有效载荷:注册数据,
服务质素:0
};
iotdata.publish(注册参数,函数(错误,数据){
if(err)console.log(err,err.stack);//发生错误
//否则发布成功!
});
setTimeout(函数(){},50);
}
//检查是否已创建所有设备
iot.listThings().on('success',函数(响应){
var things=response.data.things;
var神话=[];
for(var i=0;i

这是我扩展IOT项目的示例javascript代码。我想使用python自动创建内容、策略、证书并下载证书来完成同样的任务。之后,我想通过thing shadow将传感器数据存储到dynamo db。建议我执行此任务的正确方法。

是的,您的java脚本代码正在工作,我也希望搜索你问的同一个问题。我找到了一些有用的参考资料。

以下是python程序的完整参考资料:

物的创造

iot.create_thing(thingName="t_name")
用于创建证书并将其存储在存储库中

 with open(certname, "w") as pem_file:
            # out_file.write(things[i][thing_name])
            pem = things[i][t_name]['certificatePem']
            pem_file.write(pem)
            log.info("Thing Name: {0} and PEM file: {1}".format(
                t_name, certname))

        with open(public_key_file, "w") as pub_file:
            pub = things[i][t_name]['keyPair']['PublicKey']
            pub_file.write(pub)
            log.info("Thing Name: {0} Public Key File: {1}".format(
                t_name, public_key_file))

        with open(private_key_file, "w") as prv_file:
            prv = things[i][t_name]['keyPair']['PrivateKey']
            prv_file.write(prv)
            log.info("Thing Name: {0} Private Key File: {1}".format(
                t_name, private_key_file))
用于创建和附加策略

tp = {
    "Version": "2012-10-17",
    "Statement": [{
        "Effect": "Allow",
        "Action": [
            # "iot:*"
            "iot:Connect",
            "iot:Publish",
            "iot:Receive",
            "iot:Subscribe"
        ],
        "Resource": [
            "arn:aws:iot:{0}:*:*".format(region)
        ]
    }]
}

iot = _get_iot_session(region, cli.profile_name)
policy_name = 'policy-{0}'.format(thing_name)
policy = json.dumps(tp)
log.debug('[_create_and_attach_policy] policy:{0}'.format(policy))
p = iot.create_policy(
    policyName=policy_name,
    policyDocument=policy
)

如果你想让别人帮你写这段代码,那你就错了。你能把你的答案扩展到你已经做过的事情和你目前的困境吗?在目前的形式下,这个问题也是broad@HamzaHaider-我没有问你源代码。我没有得到任何推荐文档来完成我的任务以获得任何参考文档,推荐网站或任何视频教程,所以我问了这个问题。我是从你分享的链接中得到的。谢谢泰米尔阿拉桑