Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get all File from Subfolder Boto3

I have this code to download all the files from a buckets AWS S3

import os
import boto3

#initiate s3 resource
s3 = boto3.resource('s3')

s3 = boto3.resource(
    's3',
    aws_access_key_id = '__________',
    aws_secret_access_key = '________',
    region_name = '______'
)


# select bucket
my_bucket = s3.Bucket('MainBucket')

# download file into current directory
for s3_object in my_bucket.objects.all():
    # Need to split s3_object.key into path and file name, else it will give error file not found.
    path, filename = os.path.split(s3_object.key)
    my_bucket.download_file(s3_object.key, filename)

Inside that bucket, I have a folder called "pictures"

How can I get the files only in my folder?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My try:

s3.Bucket('MainBucket/pictures')

>Solution :

Inside that bucket, I have a folder called "pictures"

How can I get the files only in my folder?

You can get the files only in the "pictures" folder by providing a prefix:

# select bucket
my_bucket = s3.Bucket('MainBucket')

# download file into current directory
for s3_object in my_bucket.objects.filter(Prefix='pictures/'): <-- FILTER 'PICTURES'
    # Need to split s3_object.key into path and file name, else it will give error file not found.
    path, filename = os.path.split(s3_object.key)
    my_bucket.download_file(s3_object.key, filename)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading