首页 > 解决方案 > 尝试使用 AWS Lambda 将 CSV 文件从 S3 上传到 DynamoDB 时出错

问题描述

我正在尝试使用 AWS Lambda 将 CSV 文件从 S3 上传到 Amazon DynamoDB。我正在使用 Python 2.7。该文件只有 2 列:名称和 ID。代码如下:

import boto3

s3 = boto3.client('s3')
dynamodb = boto3.resource('dynamodb')

def csv_reader(event, context):

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

    obj = s3.get_object(Bucket=bucket, Key=key)

    rows = obj['Body'].read().split('\n')

    table = dynamodb.Table('test_table')

    with table.batch_writer() as batch:
        for row in rows:
            batch.put_item(Item={

                'Name':row.split(',')[0],
                'ID':row.split(',')[1]
            })

我收到此错误:

An error occurred (ResourceNotFoundException) when calling the BatchWriteItem operation: Requested resource not found: ResourceNotFoundException
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 24, in csv_reader
'ID':row.split(',')[1]
File "/var/runtime/boto3/dynamodb/table.py", line 156, in __exit__
self._flush()
File "/var/runtime/boto3/dynamodb/table.py", line 137, in _flush
RequestItems={self._table_name: items_to_send})
File "/var/runtime/botocore/client.py", line 314, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/var/runtime/botocore/client.py", line 612, in _make_api_call
raise error_class(parsed_response, operation_name)
ResourceNotFoundException: An error occurred (ResourceNotFoundException) when calling the BatchWriteItem operation: Requested resource not found

如果我尝试上传任何其他文件,我仍然会遇到同样的错误。谁能建议我应该怎么做?

标签: pythonamazon-s3aws-lambdaamazon-dynamodb

解决方案


推荐阅读