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)が問題あるようですが、何が問題なのか分かりません。
ログは以下になります。
START RequestId: 47156317-f357-4a08-8cf8-c3cf4bbf8d54 Version: $LATEST
[ERROR] ParamValidationError: Parameter validation failed:
Missing required parameter in input: "Key"
Unknown 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
Traceback (most recent call last):
File "/var/task/main.py", line 13, in handler
obj = s3.get_object(Bucket=BUCKET_NAME, key=BUCKET_KEY)
File "/var/runtime/botocore/client.py", line 386, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/var/runtime/botocore/client.py", line 677, in _make_api_call
request_dict = self._convert_to_request_dict(
File "/var/runtime/botocore/client.py", line 725, in _convert_to_request_dict
request_dict = self._serializer.serialize_to_request(
File "/var/runtime/botocore/validate.py", line 337, in serialize_to_request
raise ParamValidationError(report=report.generate_report())END RequestId: 47156317-f357-4a08-8cf8-c3cf4bbf8d54
REPORT RequestId: 47156317-f357-4a08-8cf8-c3cf4bbf8d54 Duration: 76.56 ms Billed Duration: 77 ms Memory Size: 128 MB Max Memory Used: 67 MB
"Key"で怒られているような・・。
回答1件
あなたの回答
tips
プレビュー