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.
You can create a list and append your names to it.
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')
page_iterator = paginator.paginate(Bucket='mylocals3bucketpoc')
file_list = []
for page in page_iterator:
for Contents in page['Contents']:
file_list.append(Contents['Key'])
print(file_list)
Or simple list comperehension.
file_list = [content['Key'] for page in page_iterator for content in page['Contents']]