How to list the files in S3 subdirectory using Python

Viewed 27277

I'm trying to list the files under sub-directory in S3 but I'm not able to list the files name:

import boto
from boto.s3.connection import S3Connection
access=''
secret=''
conn=S3Connection(access,secret)
bucket1=conn.get_bucket('bucket-name')
prefix='sub -directory -path'
print bucket1.list(prefix) 
files_list=bucket1.list(prefix,delimiter='/') 
print files_list
for files in files_list:
  print files.name

Can you please help me to resolve this issue.

2 Answers

You can do this by using boto3. Listing out all the files.

import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('bucket-name')
objs = list(bucket.objects.filter(Prefix='sub -directory -path'))
for i in range(0, len(objs)):
    print(objs[i].key)

This piece of code will print all the files with path present in the sub-directory

Related