In this AWS Tutorial, You will learn different ways to print and list the contents of a S3 Bucket in Python using boto3 client.
In Amazon S3, there’s a concept of a key path that can be used to mimic the folder structure just like in file structure. Keys are unique object identifiers that help in recognizng the location of some file in S3 bucket.
We can use slashes to create a folder like hierarchical structure.
Print Folders(Prefixes) in S3 Bucket
To print folders/prefixes in S3 bucket we can use boto3 client and Python. Try checking if you’ve boto3 client installed in your Amazon SDK by
import boto3
If you don’t have boto3 installed then install it by typing below code.
pip install boto3
Once you have installed boto3 on your Amazon sdk use below code to print the folders in your s3 bucket
import boto3
def list_folders_in_bucket(bucket_name):
# Create a Boto3 client for S3
s3_client = boto3.client('s3')
# Use the list_objects_v2 method to get the list of objects in the bucket
response = s3_client.list_objects_v2(Bucket=bucket_name, Delimiter='/')
# Check if the bucket has prefixes (folders)
if 'CommonPrefixes' in response:
# Loop through the prefixes and print their names (folder names)
for folder in response['CommonPrefixes']:
print(folder['Prefix'])
else:
print("Bucket is empty or contains no folders.")
if __name__ == "__main__":
# Replace 'your-bucket-name' with the actual name of your S3 bucket
bucket_name = 'your-bucket-name'
list_folders_in_bucket(bucket_name)
Print All files in a S3 Folder(prefix)
To print all files in a folder, First of all we need to create a boto3 client for s3 and then create a method to get the list of objects in a folder and check if the folder has objects or not. Then we loop through all the objects in the folder and print their keys that means print their file names. Below is the implementation of above logic to print all files in s3 folder.
import boto3
def list_files_in_folder(bucket_name, folder_name):
# Create a Boto3 client for S3
s3_client = boto3.client('s3')
response = s3_client.list_objects_v2(Bucket=bucket_name, Prefix=folder_name)
if 'Contents' in response:
for obj in response['Contents']:
print(obj['Key'])
else:
print("Folder is empty.")
if __name__ == "__main__":
# Replace 'your-bucket-name' with the actual name of your S3 bucket
# Replace 'Hiring_model2/' with the folder path you want to list
bucket_name = 'your-bucket-name'
folder_name = 'Hiring_model2/'
list_files_in_folder(bucket_name, folder_name)
In the above codes, you can replace ‘your_bucket_name’ with the name of your S3 bucket and ensure that you provide the correct folder path in folder_name. Otherwise code will print the wrong filenames in the wrong folder.
Important Notice for college students
If you’re a college student and have skills in programming languages, Want to earn through blogging? Mail us at geekycomail@gmail.com. Checkout our latest blog here Machine learning interview questions on Regularization . Follow us on Instagram.