Skip to content

Instantly share code, notes, and snippets.

@srcecde
Last active July 12, 2022 08:22
Show Gist options
  • Save srcecde/ecde0052951209731a5ed21079a416e4 to your computer and use it in GitHub Desktop.
Save srcecde/ecde0052951209731a5ed21079a416e4 to your computer and use it in GitHub Desktop.
AWS: Reading File content from S3 on Lambda Trigger
import boto3
import urllib
def lambda_handler(event, context):
s3 = boto3.client("s3")
if event:
print("Event : ", event)
file_obj = event["Records"][0]
filename = str(file_obj['s3']['object']['key'])
filename = urllib.parse.unquote_plus(filename)
print("Filename: ", filename)
fileObj = s3.get_object(Bucket = "aws-lambda-trigger", Key=filename)
print("File Obj", fileObj)
file_content = fileObj["Body"].read().decode('utf-8')
print(file_content)
return 'Thanks for Watching'
@alifanidwk
Copy link

Hi,

Thanks for sharing the code, but when I am trying to run the same, its not printing the file content, although its printing everything else but not the content. Can you please suggest anything?

Thanks
AF

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment