Reputation: 8480
I have an s3 bucket mybucket
containing three files in the following directory structure
a/b/c/d/some_file.txt
a/b/d/d/some_file2.txt
x/y/z/yet_another_file.txt
I can list all of the files using the following:
import boto3
# Extract the files from the s3 bucket
s3 = boto3.resource('s3')
bucket = s3.Bucket('mybucket')
bucket_files = [x.key for x in bucket.objects.all()]
Although this will yield all of the files in the s3 bucket, e.g.:
a/b/c/d/some_file.txt
a/b/d/d/some_file2.txt
x/y/z/yet_another_file.txt
How can I list just the files in a
? e.g.
a/b/c/d/some_file.txt
a/b/d/d/some_file2.txt
Upvotes: 1
Views: 181
Reputation: 6232
Another option:
import boto3
s3 = boto3.client('s3')
resp = s3.list_objects_v2(Bucket='mybucket', Prefix='a')
Upvotes: 0
Reputation: 16013
Use filter
with Prefix
:
import boto3
# Extract the files from the s3 bucket
s3 = boto3.resource('s3')
bucket = s3.Bucket('mybucket')
bucket_files = [x.key for x in bucket.objects.filter(Prefix='a/')]
Upvotes: 2