【问题标题】:While retrieving the bucket name from aws lambda event I am getting this error. ( "errorMessage": "'Records'" "errorType": "KeyError")从 aws lambda 事件中检索存储桶名称时,我收到此错误。 (“errorMessage”:“'Records'”“errorType”:“KeyError”)
【发布时间】:2021-06-17 20:08:53
【问题描述】:

我要运行的命令是

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']

结果:

{'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}

错误:

[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

【问题讨论】:

    标签: python amazon-web-services amazon-s3 aws-lambda boto3


    【解决方案1】:

    查看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"
                }
             }
          }
       ]
    }
    

    【讨论】:

    • @SakethNaidu 是的。我刚刚明白你的“结果”应该是什么。我会更新的。
    • 但是我怎样才能让它工作,这样它就不需要“测试”环境和触发事件的 jsons 了??
    • @SakethNaidu 您需要配置您的 S3 存储桶以将事件发送到您的 Lambda。有关于它的详细文档:docs.aws.amazon.com/AmazonS3/latest/userguide/…
    • 将我的 s3 配置为向所需的 lambda 发送事件。遵循所有提到的步骤。但是没有运气:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-20
    • 1970-01-01
    • 2022-01-23
    • 2020-07-16
    • 2017-06-07
    • 1970-01-01
    相关资源
    最近更新 更多