查看print 语句的输出,很明显该事件没有名为Records 的属性。
{
'key1': 'value1',
'key2': 'value2',
'key3': 'value3'
}
所以当你运行event['Records'] 时它不起作用。因为上面的 JSON 没有名称为 Records 的属性。
如果您有以下 JSON,这将起作用:
{
'Records': [],
'key1': 'value1',
'key2': 'value2',
'key3': 'value3'
}
如果那是 JSON,那么 events['Records'] 将返回 []。
根据您的事件 JSON,我可以判断您正在使用 AWS 控制台“测试”功能默认事件 JSON。
您需要在 AWS 控制台中为 AWS S3 事件提供正确的 JSON。
在此处记录:Event message structure
JSON 应该如下所示:
{
"Records":[
{
"eventVersion":"2.2",
"eventSource":"aws:s3",
"awsRegion":"us-west-2",
"eventTime":"The time, in ISO-8601 format, for example, 1970-01-01T00:00:00.000Z, when Amazon S3 finished processing the request",
"eventName":"event-type",
"userIdentity":{
"principalId":"Amazon-customer-ID-of-the-user-who-caused-the-event"
},
"requestParameters":{
"sourceIPAddress":"ip-address-where-request-came-from"
},
"responseElements":{
"x-amz-request-id":"Amazon S3 generated request ID",
"x-amz-id-2":"Amazon S3 host that processed the request"
},
"s3":{
"s3SchemaVersion":"1.0",
"configurationId":"ID found in the bucket notification configuration",
"bucket":{
"name":"bucket-name",
"ownerIdentity":{
"principalId":"Amazon-customer-ID-of-the-bucket-owner"
},
"arn":"bucket-ARN"
},
"object":{
"key":"object-key",
"size":"object-size",
"eTag":"object eTag",
"versionId":"object version if bucket is versioning-enabled, otherwise null",
"sequencer": "a string representation of a hexadecimal value used to determine event sequence, only used with PUTs and DELETEs"
}
},
"glacierEventData": {
"restoreEventData": {
"lifecycleRestorationExpiryTime": "The time, in ISO-8601 format, for example, 1970-01-01T00:00:00.000Z, of Restore Expiry",
"lifecycleRestoreStorageClass": "Source storage class for restore"
}
}
}
]
}