How to get all file names in a specific AWS S3 bucket directory using python ?

Published: August 22, 2023

Tags: Python; AWS; boto3; Protection Status

As part of a research project about Canada wildfires (details can be found here), I was looking for a way to retrieve a list of all file names that are stored in a particular directory within an AWS S3 bucket. For instance, I used the following NOAA directory as an example.

Table of contents

Using boto3

First, create an instance of the boto3 resource object. This object is used to interact with the AWS S3 service. Make sure that you configure your credentials correctly before continuing:

import boto3

from botocore import UNSIGNED
from botocore.config import Config

s3 = boto3.resource('s3', config=Config(signature_version=UNSIGNED))

Next, retrieve a reference to the bucket in which your files are located. You can do this by specifying the name of the bucket:

bucket_name = "noaa-nesdis-n21-pds"

s3_bucket = s3.Bucket(bucket_name)

Now, we can use s3_bucket.objects.filter(Prefix=folder).all() to retrieve a list of all the files stored in this bucket. This will return an iterator object which contains a list of all the files stored in this bucket. You can use Python's built-in list comprehension syntax to extract the file names from this list:

folder = 'VIIRS-IMG-GEO-TC/2023/05/30/'

files_in_s3 = [f.key.split(folder + "/")[0] for f in s3_bucket.objects.filter(Prefix=folder).all()]

Then in this example








By following these steps, you'll be able to get all file names in an AWS S3 bucket directory using Python. This is a great way to keep track of what files are stored.


Links Site