简体   繁体   English

从 aws lambda 事件检索存储桶名称时,我收到此错误。 ( “errorMessage”: “'Records'” “errorType”: “KeyError”)

[英]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.

相关问题 "errorMessage": "'queryStringParameters'", "errorType": "KeyError", - "errorMessage": "'queryStringParameters'", "errorType": "KeyError", AWS Lambda:Boto3“errorType”:“KeyError” - AWS Lambda: Boto3 "errorType": "KeyError" 如何将 csv 文件从 Lambda 上传到 S3 存储桶? “[ERROR] KeyError: 'Records' 并且我的 lambda 函数也没有被 s3 触发 - How can I upload the csv files from Lambda to an S3 bucket? "[ERROR] KeyError: 'Records' and my lambda function is also not getting triggered by s3 从AWS Lambda访问AWS DynamoDB时出错 - Getting error while accessing AWS DynamoDB from AWS Lambda 为什么我会收到此错误? (KeyError:'用户名') - Why am I getting this error? (KeyError: 'username') 我正在尝试将天气数据从 AWS lambda 返回到 Lex 机器人。 我遇到了keyerror - I am trying to return weather data from AWS lambda to Lex bot. I am coming across keyerror AWS Lambda Boto3 python - dynamodb 表上的过滤器表达式抛出错误“errorMessage”:“名称'Attr'未定义”, - AWS Lambda Boto3 python - filter expression on dynamodb table throw error "errorMessage": "name 'Attr' is not defined", AWS S3 中的“KeyError: 'Records'” - Lambda 触发器 - “KeyError: 'Records'” in AWS S3 - Lambda trigger 为什么在 Apply 中使用 Lambda 时出现错误 - Why I am getting Error while using Lambda within Apply 我正在尝试使用 python 从 aws 下载整个 s3 存储桶,但出现 OSError[error22] - I am trying to down load the entire s3 bucket from aws using python but getting a OSError[error22]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM