在aws中执行基于python的lambda函数时发生keyerror

时间:2019-12-26 02:22:53

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

import json
import boto3

s3_client = boto3.client('s3')
dynamodb = boto3.resource('dynamodb',region_name='ca-central-1')

def lambda_handler(event, context, verbose=True):
    bucket = event['Records'][0]['s3']['bucket']['name']
    json_filename = event['Records'][0]['s3']['object']['key']
    json_object = s3_client.get_object(Bucket=bucket, Key=json_filename)
    json_file = json_object['Body'].read()
    data_dict = json.loads(json_file)
    table = dynamodb.Table('the_dynamodb_table_name')
    with table.batch_writer() as batch:
        for rec in data_dict:
            batch.put_item(Item=rec)

尝试从s3批量上传json文件到dynamodb,但出现以下错误,不确定我是否在代码中丢失了任何内容

Extra data: line 12 column 1 - line 24 column 1 (char 480 - 683): ValueError
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 12, in lambda_handler
    data_dict = json.loads(json_file)
  File "/usr/lib64/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib64/python2.7/json/decoder.py", line 367, in decode
    raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 12 column 1 - line 24 column 1 (char 480 - 683)

1 个答案:

答案 0 :(得分:-1)

我可以找到合适的桶。您会再次检查代码吗?

    In [12]: event
    Out[12]:
    {'Records': [{'eventVersion': '2.1',
    'eventSource': 'aws:s3',
    'awsRegion': 'ca-central-1',
    'eventTime': '2019-12-25T23:13:14.473Z',
    'eventName': 'ObjectCreated:Put',
    'userIdentity': {'principalId': 'A15TZ6KQB3ZAHF'},
    'requestParameters': {'sourceIPAddress': '99.231.5.115'},
    'responseElements': {'x-amz-request-id': '812C155FDC9CBE4F',
    'x-amz-id-2': 'unaXDCdopt0bzb6diyiMxKzVUBNt1YFbEQQTAgGVmbEEZ+88GJ5qQsN9Z4x36mb9jQmIJHPOu5ohs7IYLvtnD4waXESZHs8n'},
    's3': {'s3SchemaVersion': '1.0',
        'configurationId': '1d52ab3c-7215-4bb9-89f6-6886909b0888',
        'bucket': {'name': 'json-dynamodb',
        'ownerIdentity': {'principalId': 'A15TZ6KQB3ZAHF'},
        'arn': 'arn:aws:s3:::json-dynamodb'},
        'object': {'key': 'Technical+Challenge+-+Innovation+Project.json',
        'size': 1605924,
        'eTag': 'a09aba0e2cf1b2fc57aeb1e79d979217',
        'sequencer': '005E03ED0ABBCDFE3A'}}}]}

    In [13]: bucket = event['Records'][0]['s3']['bucket']['name']

    In [14]: bucket
    Out[14]: 'json-dynamodb'