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 ?
CodePudding user response:
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'])