Read/Download S3 files using Lambda Functions
MD RAHIM IQBAL
Posted on February 15, 2023
To get started with this tutorial, you'll first need to create an Amazon S3 bucket and upload a sample object to it.
Step 1 Create a bucket and upload a sample object:
- Open the Amazon S3 console and choose "Create bucket".
- Give your bucket a name (e.g. MyTestBucket07) and choose a region.
- Click "Create bucket".
- Select the bucket you just created and go to the "Objects" tab.
- Choose "Upload" and select a test file (e.g. MyTestFile.txt) from your local machine. This file can be a text file containing anything you want.
- Click "Upload".
Once your bucket is set up, you can create a Lambda function using a function blueprint. A blueprint is a sample function that demonstrates how to use Lambda with other AWS services.
Step 2 Create an IAM role:
Create an IAM role with a policy that provides read and write access to S3
Step 3 Create the Lambda Function:
Here are the steps to create a Lambda function in the console:
- Open the Functions page on the Lambda console.
- Choose "Create function".
- Select "Use a blueprint".
- Search for "s3" under Blueprints and choose the s3-get-object blueprint for Node.js.
- Click "Configure".
- Under "Basic information",
- Enter a name for your function (e.g. ReadS3File)
- Choose "Create a new role from AWS policy templates" for the execution role.
- Enter a name for your role (e.g. S3ReadAccess).
- From Policy template select Amazon S3 object read-only permissions
- Under "S3 trigger", choose the bucket you created earlier.
- Click "Create function". Next, you can review the function code, which retrieves the source S3 bucket name and the key name of the uploaded object from the event parameter it receives. The function uses the Amazon S3 getObject API to retrieve the content type of the object.
Step 3: To view the function code in the Lambda console:
- Go to the "Code" tab while viewing your function.
- Look under "Code source".
- AWS provided code need some changes,
const AWS = require('aws-sdk');
const s3 = new AWS.S3({apiVersion: '2006-03-01'});
exports.handler = async (event, context) => {
// Set the bucket name and object key based on the file name
const bucketName = "MyTestBucket07";
const objectKey = event.filePath;
const params = {
Bucket: bucketName,
Key: objectKey
};
try {
// Read the object
const s3Object = await s3.getObject(params).promise();
console.log(s3Object.Body.toString());
return s3Object.Body.toString();
} catch (err) {
console.log(err);
throw err;
}
};
Step 4: Test the Lambda Function
- On the Code tab, under Code source, choose the arrow next to Test, and then choose Configure test events from the dropdown list.
- In the Configure test event window, do the following: Choose Create new test event named 'TestCase01'
- Add the following code in Event JSON
{
"filePath": "MyTestFile.txt"
}
- Choose Create.
- To invoke the function with your test event, under Code source, choose Test. The Execution results tab displays the response, function logs, and request ID
💖 💪 🙅 🚩
MD RAHIM IQBAL
Posted on February 15, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.