lambdaとpythonの初学者です。
python3.9
以下のコードをlambdaでテストを実行しました。
pythonファイル名:main.py
import boto3 import json from collections import OrderedDict def handler(event, context): #必要な定数を定義 BUCKET_NAME = event['bucket_name'] BUCKET_KEY = event['bucket_key'] UPLOAD_BUCKET_KEY = 'b.json' #a.json読込 s3 = boto3.client('s3') obj = s3.get_object(Bucket=BUCKET_NAME, key=BUCKET_KEY) data = json.loads(obj['Body'].read().decode('utf-8')) print(f'{BUCKET_KEY}の内容: {data}') #b.sjonのアップロード s3 = boto3.resource('s3') obj = s3.Bucket(BUCKET_NAME).Object(UPLOAD_BUCKET_KEY) data = OrderedDict(file_name=UPLOAD_BUCKET_KEY, author=data['author'], age=(data['age']+1)) res = obj.put(Body=json.dumps(data)) if res['ResponseMetadata']['HTTPStatusCode'] == 200: print(f'[SUCCESS] upload {UPLOAD_BUCKET_KEY}')
lambdaのテストイベントは以下のように記述しました。
{ "bucket_name": "lambda-test-keein", "bucket_key": "a.json" }
AWSのS3バケット内のa.jsonを読み込んで、修正を加えb.jsonとしてアップロードします。
ハンドラをmain.handler
このテストを実行したところ。以下のエラーが発生しました。
{ "errorMessage": "Parameter validation failed:\nMissing required parameter in input: \"Key\"\nUnknown parameter in input: \"key\", must be one of: Bucket, IfMatch, IfModifiedSince, IfNoneMatch, IfUnmodifiedSince, Key, Range, ResponseCacheControl, ResponseContentDisposition, ResponseContentEncoding, ResponseContentLanguage, ResponseContentType, ResponseExpires, VersionId, SSECustomerAlgorithm, SSECustomerKey, SSECustomerKeyMD5, RequestPayer, PartNumber, ExpectedBucketOwner", "errorType": "ParamValidationError", "requestId": "ed79a6d9-2510-42fe-8105-d43732124bb2", "stackTrace": [ " File \"/var/task/main.py\", line 13, in handler\n obj = s3.get_object(Bucket=BUCKET_NAME, key=BUCKET_KEY)\n", " File \"/var/runtime/botocore/client.py\", line 386, in _api_call\n return self._make_api_call(operation_name, kwargs)\n", " File \"/var/runtime/botocore/client.py\", line 677, in _make_api_call\n request_dict = self._convert_to_request_dict(\n", " File \"/var/runtime/botocore/client.py\", line 725, in _convert_to_request_dict\n request_dict = self._serializer.serialize_to_request(\n", " File \"/var/runtime/botocore/validate.py\", line 337, in serialize_to_request\n raise ParamValidationError(report=report.generate_report())\n" ] } main.pyの obj = s3.get_object(Bucket=BUCKET_NAME, key=BUCKET_KEY)が問題あるようですが、何が問題なのか分かりません。
回答1件
あなたの回答
tips
プレビュー
2022/01/20 13:51
2022/01/21 01:24
2022/01/21 16:40 編集
2022/01/22 01:55
2022/01/23 08:16