Lambda function should be triggered when any changes in S3 object with respect to file and apply those findings in anther s3 bucket

0
  1. Two s3 buckets are created in N.Virgenia region.
  2. Lambda Function created using Python
  3. file data has to be checked in bucket 1 and compare that data with file which is available in bucket 2 if changes found transfer them to bucket 2 from bucket 1 of file
  4. if there are no modifications in file or new file added -- lambda should trigger and that new should be transferred to bucket 2 Trying to implementing the above scenario getting below error, Any idea on below error please

Response { "errorMessage": "'Record'", "errorType": "KeyError", "requestId": "6be014f1-c78c-4a9b-9728-5873b1080812", "stackTrace": [ " File "/var/task/lambda_function.py", line 9, in lambda_handler\n file_obj = event["Record"][0]["s3bucket001forlambda01"]\n" ] }

Function Logs START RequestId: 6be014f1-c78c-4a9b-9728-5873b1080812 Version: $LATEST Event : {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'} [ERROR] KeyError: 'Record'

1回答
0

The key in the Event object is names Records, and not Record as you wrote. You should reference the object using event["Records"][0]["s3"]["bucket"]["name"] and event["Records"][0]["s3"]["object"]["key"].

Further more, you are testing the code from the console, so you need to provide a valid structured event object. Just choose S3-Put for the template in the Test event page.

profile pictureAWS
エキスパート
Uri
回答済み 2年前
  • import json import urllib.parse import boto3

    print('Loading function') s3 = boto3.client('s3') def lambda_handler(event, context): #print("Received event: " + json.dumps(event, indent=2))

    # Get the object from the event and show its content type
    bucket = event["Records"][0]["s3"]["bucket"]['s3bucket001forlambda01']
    key = urllib.parse.unquote_plus(event["Records"][0]["s3"]["object"]["Notes"], encoding='utf-8')
    try:
        response = s3.get_object(Bucket=bucket, Key=key)
        print("CONTENT TYPE: " + response['ContentType'])
        return response['ContentType']
    except Exception as e:
        print(e)
        print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))
        raise e
    

    Here is my code -- still getting key error

    Response { "errorMessage": "'s3bucket001forlambda01'", "errorType": "KeyError", "stackTrace": [ " File "/var/task/lambda_function.py", line 14, in lambda_handler\n bucket = event["Records"][0]["s3"]["bucket"]['s3bucket001forlambda01']\n" ] }

    Function Logs START RequestId: eb0f25c5-e96f-4db0-83b3-702a23203847 Version: $LATEST [ERROR] KeyError: 's3bucket001forlambda01' Traceback (most recent call last):   File "/var/task/lambda_function.py", line 14, in lambda_handler     bucket = event["Records"][0]["s3"]["bucket"]['s3bucket001forlambda01'] END RequestId: eb0f25c5-e96f-4db0-83b3-702a23203847

  • You should not include ['s3bucket001forlambda01'] in there. Instead you should use ["name"], just as I wrote in my example. You get the bucket name and object key from the event object. You should not provide them youself.

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ