back-new
back-new

Reputation: 121

get s3 folder files using boto3

is there any way to get s3 specific folder all files keys which have a specific combination like

But I have a specific combination now in key like

 <transaction_id>/<this could be any thing>_input.json 

I know transaction id but not sure the center part but it should always end with _input.json . how I can get keys of folder this way ?

Upvotes: 1

Views: 1602

Answers (1)

Anon Coward
Anon Coward

Reputation: 10832

You can list all objects with a common prefix with list_objects_v2. From there you can filter out to only list items with a given suffix end string, or some other pattern:

import boto3

bucket = "-example-bucket-"
prefix = "<transaction_id>/"
suffix = "_input.json"

s3 = boto3.client('s3')
paginator = s3.get_paginator('list_objects_v2')

# List all items that start with the prefix
for page in paginator.paginate(Bucket=bucket, Prefix=prefix):
    for cur in page.get("Contents", []):
        # And further filter to only items that end with the suffix
        if cur['Key'].endswith(suffix):
            # Just show the object's key
            print(cur['Key'])

Upvotes: 4

Related Questions