AWS Lambda Python boto3 - reading the content of a file on S3

Viewed 42

I am writing a lambda function that reads the content of a json file which is on S3 bucket to write into a kinesis stream. Simple requirement. In the lambda I put the trigger as S3 bucket (with name of the bucket). When I drop a file into the bucket the Lambda gets triggered but says in the cloudwatch logs can't file key name . I am able to print the file name. I don't understand

           [ERROR] NoSuchKey: An error occurred (NoSuchKey) when calling the GetObject 
           operation: The 
         specified key does not exist.

This is the code to read the file on S3

   import boto3
   import json
    #from urllib.parse import unquote_plus
   import time
   import csv
   from pprint import pprint

   s3 = boto3.client('s3')
   kinesis = boto3.client('kinesis')

   def lambda_handler(event, context):
        if event:
        # Read bucketname, filename from event record
        file_obj = event["Records"][0]
        bucketname = file_obj['s3']['bucket']['name']
         filename = file_obj['s3']['object']['key']
   
        print('bucket name is ' + str(bucketname) + ' file name is ' + str(filename))
   
        # Get referenc to file on s3 bucket
        fileObj = s3.get_object(Bucket=bucketname, Key=filename)
        #pprint(fileObj)
   
        # Convert the data in file
        file_content = fileObj["Body"].read().decode('utf-8')
  
         # put the record to kinesis
   
        kinesis.put_record(Data=bytes(file_content, 'utf-8'), 
        StreamName='LambdaSourceKinesisStream',PartitionKey='basam')
   

        return "thanks"

The file name is agent.json

1 Answers

It is working all of a sudden. no changes were made to the code

Related