我想通过HTTP使用AWS-SDK通过AWS IoT在golang上发送消息到MQTT主题 当尝试下面的代码时,它是不成功的。答复是:
ResourceNotFoundException: Not Found
status code: 404, request id: 3d2c0f11-09f6-4e86-94bf-ea877a30ebcd
以下是我使用的代码:
package main
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/iotdataplane"
"fmt"
)
func main(){
svc := iotdataplane.New(session.New(), &aws.Config {Region: aws.String("us-west-2"), Endpoint: aws.String("https://YOUR_PREFIX.iot.us-west-2.amazonaws.com")})
params := &iotdataplane.PublishInput{
Topic: aws.String("mytopic"), // Required
Payload: []byte("PAYLOAD"),
Qos: aws.Int64(0),
}
resp, err := svc.Publish(params)
if err != nil {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
return
}
// Pretty-print the response data.
fmt.Println(resp)
我还验证了我的AWS账户验证了有效的凭据设置和策略。以下是〜/ .aws / credentials
中的aws凭据[default]
aws_access_key_id = YOUR_ACCESS_KEY_ID
aws_secret_access_key = YOUR_SECRET_ACCESS_KEY
和政策附加到您的身份:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:*"
],
"Resource": "*"
}
]
}
我也试过Node.JS它有效!!以下是Node中的WORKING源以供参考:
var AWS = require('aws-sdk');
AWS.config.update({region: 'us-west-2'});
var iotdata = new AWS.IotData({endpoint: 'YOUR_PREFIX.iot.us-west-2.amazonaws.com'});
var params = {
topic: 'mytopic', /* required */
payload: new Buffer('hello') || 'STRING_VALUE',
qos: 0
};
iotdata.publish(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
对于Node JS的工作原理,它需要在根文件夹中有适当的AWS.config文件。像这样:
accessKeyId = YOUR_ACCESS_KEY_ID
secretAccessKey = YOUR_SECRET_ACCESS_KEY
答案 0 :(得分:0)
如果仍然与任何人相关... 在aws.Config中使用凭据* credentials.Credentials
在以下位置查看文档: https://godoc.org/github.com/aws/aws-sdk-go/aws#Config