title: How to add s3 trigger function in the lambda function to read the file
body: <p>I need to add a s3 trigger in the lambda function in the source code itself instead of creating a trigger in the aws console. I need that trigger to read a file when it is uploaded on a particular folder of S3 bucket. I have done this using the creating the s3 trigger in the console itself with help of prefix. Can someone help with of creating this s3 trigger in the lambda function source code itself. Below is the source code of the lambda function for reading the file.</p>
  <pre><code>import json
  import urllib.parse
  import boto3
  
  print('Loading function')
  
  s3 = boto3.client('s3')
  
  
  def lambda_handler(event, context):
      #print(&quot;Received event: &quot; + json.dumps(event, indent=2))
  
      # Get the object from the event and show its content type
      bucket = event['Records'][0]['s3']['bucket']['name']
      key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')
      try:
          response = s3.get_object(Bucket=bucket, Key=key)
          print(&quot;CONTENT TYPE: &quot; + 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
                
  </code></pre>