如何在aws java sdk中为iot规则设置Actions?

时间:2016-10-19 09:00:25

标签: java amazon-web-services aws-iot

我正在尝试使用aws sdk for java创建规则(而不是独立的aws iot java sdk)。

到目前为止,我已经完成了这些

public class Application extends Controller {

    static AWSIotClient awsIotClient;

    private static void init() {
        AWSCredentials credentials = null;
        try {
            credentials = new ProfileCredentialsProvider().getCredentials();
        } catch (Exception e) {
            throw new AmazonClientException(
                    "Cannot load the credentials from the credential profiles file. " +
                            "Please make sure that your credentials file is at the correct " +
                            "location (~/.aws/credentials), and is in valid format.",
                    e);
        }
        awsIotClient = new AWSIotClient(credentials);
        Region usEast1 = Region.getRegion(Regions.US_EAST_1);
        awsIotClient.setRegion(usEast1);
        Logger.info("Creds init");
    }

    public static Result index() {
        init();
        CreateTopicRuleRequest another_test = new CreateTopicRuleRequest();
        another_test.setRuleName("test");
        TopicRulePayload topicRulePayload = new TopicRulePayload();
        topicRulePayload.setDescription("A test rule");
        topicRulePayload.setSql("SELECT state.reported.turbineStatus FROM '$aws/things/turbine1/shadow/update/accepted' WHERE state.reported.turbineStatus=true");
        topicRulePayload.setActions(); // stuck here
        another_test.setTopicRulePayload(topicRulePayload);
        CreateTopicRuleResult test = awsIotClient.createTopicRule(another_test);

        return ok(index.render("iot"));
    }

}

在上面的代码中,我被困在topicRulePayload.setActions();

部分

如何设置动作?我查看了它的源代码,发现它正在使用集合。

1 个答案:

答案 0 :(得分:0)

我终于让它为我工作

public static Result index() {
        init();
        CreateTopicRuleRequest another_test = new CreateTopicRuleRequest();
        another_test.setRuleName("test");
        TopicRulePayload topicRulePayload = new TopicRulePayload();
        topicRulePayload.setDescription("A test rule");
        topicRulePayload.setSql("SELECT state.reported.turbineStatus FROM '$aws/things/turbine1/shadow/update/accepted' WHERE state.reported.turbineStatus=true");
        ArrayList<Action> actionList = new ArrayList();
        Action action = new Action();
        S3Action s3 = new S3Action();
        s3.setRoleArn("arn:aws:iam::442759113924:role/sushant");
        s3.setBucketName("bajra-iot-test");
        s3.setKey("bajra-iot-test");

        action.setS3(s3);
        actionList.add(action);
        topicRulePayload.setActions(actionList);
        another_test.setTopicRulePayload(topicRulePayload);
        CreateTopicRuleResult test = awsIotClient.createTopicRule(another_test);

        return ok(index.render("iot"));
    }