AWS Lambda函数 - 无法调用更新事物阴影

时间:2016-03-16 17:16:12

标签: json amazon-web-services aws-lambda boto3 aws-iot

根据这里的boto3文档:https://boto3.readthedocs.org/en/latest/reference/services/iot-data.html#client update_thing_shadow方法采用thingName& JSON有效负载作为参数。目前它的内容如下:

    client = boto3.client('iot-data', region_name='us-east-1')
    data = {"state" : { "desired" : { "switch" : "on" }}}
    mypayload = json.dumps(data)
    response = client.update_thing_shadow(
        thingName = 'MyDevice', 
        payload = b'mypayload'
    )

当我使用命令行时,没有任何问题,但似乎无法从lamba函数中找到它。我用很多版本的代码(json.JSONEncoder,bytearray()等)调用它,没有任何运气。调用UpdateThingShadow操作时,错误的范围从语法到(ForbiddenException):错误请求:ClientError。有没有人在AWS lambda函数中成功调用此方法或类似方法?感谢。

2 个答案:

答案 0 :(得分:1)

此代码对我来说很好:

def set_thing_state(thingName, state):
    # Change topic, qos and payload
    payload = json.dumps({'state': { 'desired': { 'property': state } }})

    logger.info("IOT update, thingName:"+thingName+", payload:"+payload)
    #payload = {'state': { 'desired': { 'property': state } }}


    response = client.update_thing_shadow(
        thingName = thingName, 
        payload =  payload
        )

    logger.info("IOT response: " + str(response))  
    logger.info("Body:"+response['payload'].read())


def get_thing_state(thingName):

    response = client.get_thing_shadow(thingName=thingName)

    streamingBody = response["payload"]
    jsonState = json.loads(streamingBody.read())

    print jsonState
    #print jsonState["state"]["reported"]

祝你好运

答案 1 :(得分:0)

garnaat是对的。 只需将payload = b'mypayload'替换为payload = mypayload即可。