I have multiple files uploaded in S3 bucket and I want to print the names of the files in the output in a list through lamda python .
Lets say I have files S1,S2,S3 … and so on and I want the output as
[‘S1′,’S2′,’S3’..] for all the files uploaded in S3.
I have tried code like below but not able to print the output as expected :-
import boto3
import json
from datetime import datetime as dt
client = boto3.resource('s3')
s3_client = boto3.client('s3')
def lambda_handler(event, context):
paginator = s3_client.get_paginator('list_objects')
# Create a PageIterator from the Paginator
page_iterator = paginator.paginate(Bucket='mylocals3bucketpoc')
for page in page_iterator:
vars = []
for Contents in page['Contents']:
print(Contents['Key'])
The output :-
S1
S2
S3
as I want to have the output like ['S1','S2','S3']
,I am trying to use list append but not able to get desired output.
Please help me to resolve the same.
2
Answers
You can create a list and append your names to it.
Or simple list comperehension.
To achieve this in an AWS Lambda function, you can use the following code. This code lists all objects in the specified S3 bucket, extracts the filenames, and prints them in a list format.
Make sure you have the appropriate IAM role permissions for your Lambda function to access the S3 bucket.
Use the boto3 library to interact with S3.
Here’s the code for your Lambda function:
def lambda_handler(event, context):
# Initialize the S3 client
s3_client = boto3.client(‘s3’)
Explanation
Initialize the S3 Client: The Lambda function initializes an S3 client to communicate with S3.
List Objects: The list_objects_v2 method is called to retrieve the list of objects in the specified bucket.
Extract and Append Filenames: For each object, the filename (i.e., the object Key) is appended to the filenames list.
Print and Return: The filenames list is printed (for viewing in Lambda logs) and returned as the function’s output.
Important Points
IAM Permissions: Ensure the Lambda function’s execution role has s3:ListBucket permissions for the bucket.
Environment Setup: Replace ‘your-bucket-name’ with the actual name of your S3 bucket.
With this setup, when you upload files like S1, S2, S3 to the bucket, invoking this Lambda function will output:
[‘S1’, ‘S2’, ‘S3’, …]
Know more details-
https://reactnativelab.blogspot.com/