Borealis
Borealis

Reputation: 8480

How to isolate files in S3 bucket by directory?

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

Answers (2)

Carlos Rendon
Carlos Rendon

Reputation: 6232

Another option:

import boto3
s3 = boto3.client('s3')
resp = s3.list_objects_v2(Bucket='mybucket', Prefix='a')

Upvotes: 0

Jordon Phillips
Jordon Phillips

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

Related Questions