美文网首页
AWSIOT java与go客户端权限获取

AWSIOT java与go客户端权限获取

作者: SailSea | 来源:发表于2020-03-03 12:05 被阅读0次

java

pom.xml

        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk</artifactId>
            <version>1.11.645</version>
        </dependency>

import

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.dlm.model.CreateRule;
import com.amazonaws.services.iot.AWSIot;
import com.amazonaws.services.iot.AWSIotClientBuilder;
import com.amazonaws.services.iot.model.* ;

获取客户端函数

    private static AWSCredentials getAwsCredentials() {
        return new BasicAWSCredentials("123213RVOHIW5JG5BWYQP6", "6/dfUUDAmE2resQ1loDEFZ5GtHi4YOOSq3a123213");
    }

    public static AWSIot awsIotClient() {
        return AWSIotClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(getAwsCredentials()))
                .withRegion("us-west-2").build();
    }

go

package main

import (
    "encoding/json"
    "fmt"
    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/credentials"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/iot"
    "log"
    "os"
)
func newAWSConfig() *aws.Config {
    c := aws.NewConfig()
    return c
}

var (
    //svc iotiface.IoTAPI
    listThingType string

    AccessKeyID = "A13121VOHwdIW5JG5BWYQP6"
    SecretAccessKey = "6/dfUUDAmEwd2resQ1lo12213tHi4YOOSq3a4NbG7"
)

func buildListThings() *iot.ListThingsInput {
    lt := &iot.ListThingsInput{}

    if listThingType != "" {
        lt.AttributeName = aws.String("Type")
        lt.AttributeValue = aws.String(listThingType)
    }

    return lt
}

func main() {

    var client = session.Must(session.NewSession(
        aws.NewConfig().
            WithRegion("us-west-2").
            WithEndpoint("https://a1231231v9-ats.iot.us-west-2.amazonaws.com").
            WithLogLevel(3),
    ))

    //赋值AccessKeyID  SecretAccessKey 添加权限,morn读取本地配置文件~/.aws/credentials信息设置权限
    client.Config.Credentials = credentials.NewStaticCredentials(AccessKeyID, SecretAccessKey, "")
    if err := ListIoTThings(client);err != nil {
        fmt.Println("Error",err)
        os.Exit(1)
    }

    svc := iot.New(client, &aws.Config {})
    
    resp, err := svc.ListThings(buildListThings())
    if err != nil {
        log.Println("Error",err)
        os.Exit(1)
    }
    
    b, _ := json.MarshalIndent(resp, "", "  ")
    fmt.Fprintf(os.Stdout, "%s\n", string(b))
}


func ListIoTThings(sess *session.Session) ( error) {
    svc := iot.New(sess)

    //params := &iot.ListThingsInput{
    //  MaxResults: aws.Int64(100),
    //}

    params := &iot.ListThingTypesInput{

    }

    for {
        output, err := svc.ListThingTypes(params)
        if err != nil {
            return err
        }

        for _, thing := range output.ThingTypes {
            fmt.Println("ThingTypeName",thing.ThingTypeName)
        }

        if output.NextToken == nil {
            break
        }
        params.NextToken = output.NextToken
    }

    return nil
}

go参考:github.com/alesr/awsiot-thingcreator

相关文章

网友评论

      本文标题:AWSIOT java与go客户端权限获取

      本文链接:https://www.haomeiwen.com/subject/ltbqlhtx.html