python 版本的可以参考:https://blog.csdn.net/qq_33811662/article/details/80732275
首先需要一个 ListThings 权限 AWS 的 IAM 帐号,需要获取 access_key 与 secret_key。
需要引用一个 iot 包,此处我是使用 Maven 构建的项目,在 pom.xml 中加入
<dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-iot</artifactId> <version>1.11.347</version> </dependency>
接下去就是代码部分:
import java.util.List; import com.amazonaws.auth.AWSCredentials; import com.amazonaws.regions.Region; import com.amazonaws.regions.Regions; import com.amazonaws.services.iot.AWSIot; import com.amazonaws.services.iot.AWSIotClient; import com.amazonaws.services.iot.model.ListThingsRequest; import com.amazonaws.services.iot.model.ListThingsResult; import com.amazonaws.services.iot.model.ThingAttribute; public class IoTTest { static AWSIot iot; private static String AWS_ACCESS_KEY = "xxxxxxxx"; // 你的 access_key private static String AWS_SECRET_KEY = "xxxxxxxx"; // 你的 secret_key static { iot = new AWSIotClient(); iot.setRegion(Region.getRegion(Regions.US_EAST_1)); // 根据自己的地区调整 } public static void main(String[] args) { AWSCredentials credentials = new AWSCredentials() { @Override public String getAWSSecretKey() { return AWS_SECRET_KEY; } @Override public String getAWSAccessKeyId() { return AWS_ACCESS_KEY; } }; ListThingsRequest listThingsRequest = new ListThingsRequest(); listThingsRequest.setRequestCredentials(credentials); ListThingsResult listThingsResult = iot.listThings(listThingsRequest); List<ThingAttribute> list = listThingsResult.getThings(); list.forEach(e -> System.out.println(e.getThingName())); } }