Skip to content

Instantly share code, notes, and snippets.

@JoeShi
Created July 19, 2019 09:52
Show Gist options
  • Save JoeShi/91b1d5d552d46ced28f22cf8d16b2a4d to your computer and use it in GitHub Desktop.
Save JoeShi/91b1d5d552d46ced28f22cf8d16b2a4d to your computer and use it in GitHub Desktop.
AWS IoT Client JAVA example

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.example.basicapp</groupId>
  <artifactId>myapp</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>myapp</name>
  <url>http://maven.apache.org</url>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-bom</artifactId>
        <version>1.11.327</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-java-sdk-iot</artifactId>
    </dependency>
  </dependencies>
</project>

App.java

package org.example.basicapp;

import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.iot.AWSIot;
import com.amazonaws.services.iot.AWSIotClientBuilder;
import com.amazonaws.services.iot.model.CreateThingRequest;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        AWSIot iotClient = AWSIotClientBuilder.standard()
                .withRegion(Regions.CN_NORTH_1)
                .withCredentials(new ProfileCredentialsProvider("zhy")) // remove this line if using default profile
                .build();
        try {
            iotClient.createThing(new CreateThingRequest().withThingName("zhangsan"));
        } catch (Exception e) {
            System.out.println("IotServiceUtil.createIotService aws-iot创建连接异常"+ e);
        }
        System.out.println( "Hello World!" );
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment