I was able to assign the sns event data to a variable using
我能够使用sns将事件数据分配给变量
def lambda_handler(event, context):
message = event['Records'][0]['Sns']['Message']
print("From SNS: " + message)
Output :
{
"Records": [
{
"eventVersion": "2.0",
"eventSource": "aXXXX",
"awsRegion": "XXXXX",
"eventTime": "2016-03-09T12:24:19.255Z",
"eventName": "ObjectCreated:Put",
"userIdentity": {
"principalId": "AWS:XXXXXXXXXXX"
},
"requestParameters": {
"sourceIPAddress": "xxx.xxx.xx.xx"
},
"responseElements": {
"x-amz-request-id": "XXXX",
"x-amz-id-2": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
},
"s3": {
"s3SchemaVersion": "1.0",
"configurationId": "xxx-xxx-xxx",
"bucket": {
"name": "bucketname",
"ownerIdentity": {
"principalId": "XXXXXX"
},
"arn": "arn:aws:s3:::xxxxx"
},
"object": {
"key": "index.js",
"size": 7068,
"eTag": "xxxx",
"sequencer": "0000000000"
}
}
}
]
}
I am unable to further parse and get the values of awsRegion
, Records.s3.bucket.name
and Records.s3.object.key
.
我无法进一步解析并获取awsRegion,Records.s3.bucket.name和Records.s3.object.key的值。
I have tried bucketname = message['Records'][0]['s3']['bucket']['name']
. getting the error TypeError: string indices must be integers
我尝试过bucketname = message ['Records'] [0] ['s3'] ['bucket'] ['name']。得到错误TypeError:字符串索引必须是整数
1 个解决方案
#1
9
I think you may need to load the json:
我想你可能需要加载json:
import json
def lambda_handler(event, context):
message = event['Records'][0]['Sns']['Message']
parsed_message = json.loads(message)
print(parsed_message['Records'][0]['s3']['bucket']['name'])
Gives me
u'bucketname'
Or are you doing the loads somewhere outside your function?
或者你在功能之外的某个地方做负荷?
#1
9
I think you may need to load the json:
我想你可能需要加载json:
import json
def lambda_handler(event, context):
message = event['Records'][0]['Sns']['Message']
parsed_message = json.loads(message)
print(parsed_message['Records'][0]['s3']['bucket']['name'])
Gives me
u'bucketname'
Or are you doing the loads somewhere outside your function?
或者你在功能之外的某个地方做负荷?