[英]While retrieving the bucket name from aws lambda event I am getting this error. ( “errorMessage”: “'Records'” “errorType”: “KeyError”)
The command I'm trying to run is我试图运行的命令是
def lambda_handler(event, context):
print(event)
#retrieve bucket name and file_key from the S3 event
bucket_name = event['Records'][0]['s3']['bucket']['name']
file_key = event['query']['Records'][0]['s3']['object']['key']
The result:结果:
{'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
The error:错误:
[ERROR] KeyError: 'Records'
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 12, in lambda_handler
bucket_name = event['Records'][0]['s3']['bucket']['name']
END RequestId: 29150087-77bd-49db-a88b-61eb8a1645fd
Looking at the output of your print
statement, it is clear that the event does not have a property called Records
.查看
print
语句的输出,很明显该事件没有名为Records
的属性。
{
'key1': 'value1',
'key2': 'value2',
'key3': 'value3'
}
So when you run event['Records']
it does not work.因此,当您运行
event['Records']
它不起作用。 Because the JSON above does not have a property with name Records
.因为上面的 JSON 没有名称为
Records
的属性。
This would work if you have the following JSON:如果您有以下 JSON,这将起作用:
{
'Records': [],
'key1': 'value1',
'key2': 'value2',
'key3': 'value3'
}
If that would be the JSON, then events['Records']
would return []
.如果那是 JSON,那么
events['Records']
将返回[]
。
Based on the JSON of your event, I can tell that you are using the AWS Consoles "Test" features default event JSON.根据您事件的 JSON,我可以判断您正在使用 AWS 控制台“测试”功能默认事件 JSON。
You need to provide the proper JSON in the AWS console for AWS S3 Events.您需要在 AWS 控制台中为 AWS S3 事件提供正确的 JSON。
It is documented here: Event message structure它记录在这里: 事件消息结构
The JSON should look like this: 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"
}
}
}
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.