简体   繁体   English

AWS Lambda python function 从 S3 解析 json 并存储在 DynamoDB

[英]AWS Lambda python function to parse json from S3 and store in DynamoDB

I'm trying to write a lambda function that is triggered whenever a json file is uploaded to an s3 bucket.我正在尝试编写一个 lambda function ,只要将 json 文件上传到 s3 存储桶就会触发。 The function is supposed to parse the file and store it immediately in DynamoDB. function 应该解析文件并将其立即存储在 DynamoDB 中。 I created a table called 'data' with the primary key set as 'date'.我创建了一个名为“数据”的表,主键设置为“日期”。 Here's what I have for the function so far:到目前为止,这是我对 function 的了解:

import boto3
import json
s3_client = boto3.client('s3')
dynamodb = boto3.resource('dynamodb')

def lambda_handler(event, context): 
   bucket = event['Records'][0]['s3']['bucket']['name']
   json_file_name = event['Records'][0]['s3']['object']['key']
   json_object = s3.Bucket(bucket).Object(json_file_name)
   jsonFileReader = json_object['Body'].read()
   jsonDict = json.loads(jsonFileReader)
   table = dynamodb.Table('data')
   table.put_item(Item = jsonDict)

Here is an example of a json file I'm trying to use:这是我尝试使用的 json 文件的示例:

{
 "date": "2020-06-07 21:00:34.284421",
 "ConfirmedCases": 7062067,
 "ActiveCases": 3206573,
 "RecoveredCases": 3450965,
 "Deaths": 404529
}

Unfortunately, whenever I test the code, it throws this error:不幸的是,每当我测试代码时,它都会抛出这个错误:

[[ERROR] TypeError: string indices must be integers
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 7, in lambda_handler
    bucket = event'Records'][0]['s3']['bucket']['name']]

Does anyone know how to resolve this issue?有谁知道如何解决这个问题? I've wasted so much time trying to figure this out and I still am unable to:/我浪费了很多时间试图弄清楚这一点,但我仍然无法:/

Your error is from either of these lines.您的错误来自这些行中的任何一个。

bucket = event['Records'][0]['s3']['bucket']['name']
json_file_name = event['Records'][0]['s3']['object']['key']

However, they are correct .但是,它们是正确的。 This is the valid way of access bucket name and object key from the event generated by S3 Notifications.这是从 S3 Notifications 生成的事件中访问存储桶名称和 object 密钥的有效方式

It seems to me that something else is trigger your function .在我看来,还有其他东西会触发您的 function Either you use Test option in the console and provide incorrect event object, or there is some other event that triggers the lambda with non-S3 event.您在控制台中使用Test选项并提供不正确的event object,或者有一些其他事件会触发带有非 S3 事件的 lambda。

As a quick fix, you can do the following.作为快速修复,您可以执行以下操作。 The code below will finish if event object does not contain Records :如果event object 不包含Records ,则以下代码将完成:

def lambda_handler(event, context): 

    if 'Records' not in event:
        print(event) # good to check what event you get  
        return

    # and the rest of code  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Merging multiple JSON files into single JSON file in S3 from AWS Lambda python function - Merging multiple JSON files into single JSON file in S3 from AWS Lambda python function 使用 Lambda function (python) 将 DynamoDB 导出到 S3 上的 CSV - Export DynamoDB to CSV on S3 with Lambda function (python) AWS lambda:在本地调用可访问 s3 的 python lambda 函数 - AWS lambda: Invoking locally a python lambda function with access to s3 AWS Lambda (Python) 无法在 S3 中解压缩和存储文件 - AWS Lambda (Python) Fails to unzip and store files in S3 如何在 S3 上存储大型 Python 依赖项(适用于无服务器的 AWS Lambda) - How to Store Large Python Dependencies on S3 (for AWS Lambda with Serverless) Python Lambda Function 解析DynamoDB的Z0ECD11CFormat1D7A287401D148A23BBD - Python Lambda Function Parsing DynamoDB's JSON Format 使用上传到 S3 的 json 文件中的输入参数触发 AWS lambda function - Trigger AWS lambda function using input parameter from json file uploaded to S3 AWS Lambda从S3以HTML格式显示DynamoDB中的内容 - AWS Lambda load content from DynamoDB display in HTML from S3 使用AWS lambda函数使用boto3 python将S3文件从zip转换为gzip - Use AWS lambda function to convert S3 file from zip to gzip using boto3 python 从 AWS lambda function 中的 s3 存储桶中读取 .mdb 或 .accdb 文件并使用 python 将其转换为 excel 或 csv - Reading .mdb or .accdb file from s3 bucket in AWS lambda function and converting it into excel or csv using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM