Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get s3 files with prefix using python

My s3 filename is 'folder/filename.xml'. i want to take the files end with 'name.xml'

import boto3
s3 = boto3.resource('s3')
try:
fileobj = s3.Object('lcu-matillion',''folder/.*name.xml'').get()['Body']

data=fileobj.read()
except Exception:
  print('not found')    

Any one please help with accurate code? Thanks

like image 423
Ganesh Pitchai Avatar asked Aug 31 '26 10:08

Ganesh Pitchai


1 Answers

Don't forget that there could be multiple files that match that wildcard.

You would use something like:

import boto3

s3 = boto3.resource('s3', region_name='ap-southeast-2')

bucket = s3.Bucket('my-bucket')

objects = bucket.objects.filter(Prefix='folder-name/')

for object in objects:
  if object.key.endswith('.txt'):
    object.download_file('/tmp/' + object.key)
like image 189
John Rotenstein Avatar answered Sep 02 '26 23:09

John Rotenstein



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!