How to get list of files in S3 bucket

Hi,
In the assignment, the file path in the S3 bucket was provided.
Is there a way to view the list of files with the path in the S3 bucket, in python SDK?

Thanks and Regards
Aroonima

Hello @Aroonima!

First of all, I’m not mentor of PDS Specialization, So what I’m going to say now it’s about my practical usage of AWS S3 and it can be a little different in the Specialization.

The python SDK for AWS is boto3. To list files with it use one of the commands bellow:

# Using client
import boto3

conn = boto3.client('s3')  # assumes boto.cfg setup, assume AWS S3
for key in conn.list_objects(Bucket='bucket_name')['Contents']:
    print(key['Key'])
# Using resource
import boto3
s3 = boto3.resource('s3')

my_bucket = s3.Bucket('bucket_name') # assumes boto.cfg setup, assume AWS S3

for file in my_bucket.objects.all():
    print(file.key)

Did it help you?

Bets regards,
Wesley P.

Hi @wesleyalmeida!

Thanks for sharing the code. Indeed very helpful…

Regards
Aroonima