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?
My try:
s3.Bucket('MainBucket/pictures')
CodePudding user response:
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)