Iterate in bucket folders AWS

Viewed 25

Hi I am new at AWS and Lambda.

I have bucket s3 called RootBucket and inside this bucket I have the next structure:

RootFolder:

ChildFolder1:
    versionFolder1
          file1.txt
          file2.txt
          file3.txt
    versionFolder2
          file1.txt
          file2.txt
          file3.txt
ChildFolder2:
    versionFolder1 
          file1.txt
          file2.txt
          file3.txt
    versionFolder2
          file1.txt
          file2.txt
          file3.txt

I have to create a lambda function that receives a json and inside of this json I have keys:

{ 'fileName': 'file1.txt',

'folder': 'childFolder1',

'versionFolder': 'versionFolder1',

'fileContent': '.....',

}

So i have to iterate over the bucket folders so i made this:

def lambda_handler(jsonBody):

    body = BytesIO(event['fileContent'].encode())
    name = jsonBody['fileName']
    childFolder = jsonBody['folder']
    version = jsonBody['versionFolder']

    rootFolder = client.get_object(
                 Bucket = 'rootFolder',
                 Key = childFolder
    )


    all_objects = rootFolder.list_objects_v2(Bucket=rootFolder, Prefix=childFolder)
    for a in all_objects['Contents']:
        if 'Contents' in a:
            print(a)

I want to iterate over each folder based on the json content. But the problem is that it iterates over the folder and the folder content. I need a way to iterate based on the json content. Could anybody help me?

0 Answers
Related