How To Read AWS S3 File Using Lambda Function (2 Min)

preview_player
Показать описание
In this tutorial, you'll learn how to read AWS S3 file using a lambda function.



Video Transcript:

Hi guys, this is Abi from Gokcedb. In this video, you’re going to learn how to read an AWS S3 file using a Lambda function. Let’s start by searching for S3 on AWS Console and then click on the first search result.

Next, click on Create Bucket and give it a name. I’m going to leave all the settings to default and then hit the create button. Now, go inside my AWS Bucket and click on the upload button.

Click on Create function and give it a name. For runtime, I’m going to select Python and for Default Execution Role, I’m going to select Create a New Role from AWS Policy Templates. Give the Role a name and from Policy Templates, search for Amazon S3 Object Read-Only Permission.

Hit the Create function button and scroll down to the Code Source Section. Here, I’m going to copy-paste my pre-written Python Code. On line 3, I’m importing border 3 which is the SDK library for AWS.

On line 4, I’m defining the S3 client. And lines 7 and 8, I’m defining the Bucket name and the File name. On line 10, I’m using the Get Underscore Object Method to get the response back from the S3 Client.

On line 13, I’m parsing the response body and reading the file data. Finally, on line 16, I’m returning the file data in all uppercase. Next, let’s copy and paste this code into the Lambda underscore function and hit deploy.

Click on test and give this Test Event a name. Save and hit test again to test your Lambda function. As expected, we got the hello world response pack in all caps.

Next, click on configuration and choose the function URL. Click an authentication type. I’m going to select none and hit save.

Now you should see a function URL assigned to your Lambda function which you can access through any browser. There you have it. Make sure you like, subscribe, and turn on the notification bell. Until next time.

import json
import boto3

def lambda_handler(event, context):
bucket_name = 'myawsbucket1098'

print("s3_response:", s3_response)

file_data = s3_response["Body"].read().decode('utf')
print("file_data:", file_data)

return str(file_data).upper()
Рекомендации по теме
Комментарии
Автор

I am amazed by your teaching method. the way you present, the way you talk, and the best part is you pay attention to details which is not seen often around.

SaddamHussain-lfrg
Автор

Hello! Gokce DB
Your tutorial is very informative... fabulous...👌

bert
Автор

I have deployed my django project on Vercel. I am not able to use the imageField as the Vercel is a readonly file system. So can you make video on that in which all the images gets upoaded on s3-bucket and get the links in django

forwardtechnical
Автор

I wonder if data transfer is free when downloading files from s3 into lambda using built in aws sdk. Someone says its not free unless i configure vpc endpoint which means using pravate ip

frontend_ko
Автор

in browser i get:

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

but if i do curl on my url than its ok

could you expand on that?

makondoo
Автор

Could you pls tell me how to read an S3 CSV file to lambda

nagamahesh
Автор

I am interested to your teaching method sir{Thank You}

multanvoice