简体   繁体   English

从 lambda 函数 [Java] 发布到 AWS iot 上的主题

[英]Publishing to an topic on AWS iot from lambda function [Java]

I'm trying to publish on a topic on my AWS Iot from my lambda function which is triggered by an alexa skill.我正在尝试通过我的 lambda 函数在我的 AWS Iot 上发布一个主题,该函数由 alexa 技能触发。 Which class of AWSClient is the right one to do so?哪一类AWSClient适合这样做?

Based on an answer on stackoverflow i know that i need to use the HTTP method to publish from an aws lambda function to aws iot, rather than MQTT .根据 stackoverflow 上的答案,我知道我需要使用HTTP方法从 aws lambda 函数发布到 aws iot,而不是MQTT As the class AWSIotDataClient is deprecated, I don't know which class to use.由于AWSIotDataClientAWSIotDataClient被弃用,我不知道要使用哪个类。 It is suggested by AWS to use AwsIotClientBuilder , which I did but what now? AWS 建议使用AwsIotClientBuilder ,我做了但现在怎么办?

    AWSIotClientBuilder client =  AWSIotClientBuilder.standard();
    client.setEndpointConfiguration(conf);
    client.setCredentials(new AWSCredentialsProvider() {
        @Override
        public AWSCredentials getCredentials() {
            return cred;
        }

        @Override
        public void refresh() {

        }
    });

AWSIotDataClient is not deprecated, just the constructors are deprecated, as are the constructors of all AWSClient implementations in favor of builders. AWSIotDataClient没有被弃用,只是构造函数被弃用,所有AWSClient实现的构造函数也被弃用,支持构建器。 You should use AwsClientBuilder.build() to obtain an instance of AWSIotDataClient .您应该使用AwsClientBuilder.build()来获取AWSIotDataClient的实例。 Then you can call the publish() method on the AWSIotDataClient instance to publish to your IoT topic.然后,您可以在AWSIotDataClient实例上调用publish()方法以发布到您的 IoT 主题。

AWSIotData awsIotDataClient = AWSIotDataClientBuilder.defaultClient(); // add your AWS creds to environment vars to test locally

    awsIotDataClient.publish(new PublishRequest()
                                     .withPayload(ByteBuffer.wrap(("{\"some\":\"message\"}").getBytes()))
                                     .withQos(1)
                                     .withTopic("your/topic"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM