• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Check if File Exists in AWS S3 Bucket Using Python

Check if File Exists in AWS S3 Bucket Using Python

February 8, 2023 Leave a Comment

To check if a file exists in an AWS S3 bucket, the easiest way is with a try/except block and using the boto3 get_object() function.

import boto3

s3c = boto3.client('s3', region_name="us-east-2",aws_access_key_id="YOUR AWS_ACCESS_KEY_ID",aws_secret_access_key="YOUR AWS_SECRET_ACCESS_KEY")

try:
    obj = s3c.get_object(Bucket="YOUR-BUCKET",Key="FILENAME")
    result = obj["Body"].read()
except s3c.exceptions.NoSuchKey:
    #File doesn't exist
    pass

When working with files in your programs, sometimes you need to be able to easily check if a file exists.

One example of this is if you have your data stored in an AWS S3 bucket and you want to make sure the file exists.

In Python, the boto3 package allows you to easily interact with and create, configure and manage AWS services.

To start, you need to connect to AWS. This is done by first using the boto3 client() function. You should pass your access key and secret access key here to authenticate.

Then, to check if a file exists in the bucket, you should use a try/except block.

Inside the try/except block, you shoud use the boto3 get_object() function. If the function get_object() is able to get the object, there will be no exception and you will be able to continue. If there is an exception, then your program will not error out and you can handle the exception.

Below shows the full code for checking if a particular key exists in an AWS S3 bucket.

import boto3

s3c = boto3.client('s3', region_name="us-east-2",aws_access_key_id="YOUR AWS_ACCESS_KEY_ID",aws_secret_access_key="YOUR AWS_SECRET_ACCESS_KEY")

try:
    obj = s3c.get_object(Bucket="YOUR-BUCKET",Key="FILENAME")
    result = obj["Body"].read()
except s3c.exceptions.NoSuchKey:
    #File doesn't exist
    pass

Hopefully this article has been useful for you to learn how to check if a file exists in an AWS S3 bucket using Python.

Other Articles You'll Also Like:

  • 1.  Using Python to Convert Float to Int
  • 2.  pandas Absolute Value – Get Absolute Values in a Series or DataFrame
  • 3.  Draw Rectangle in Python Using turtle Module
  • 4.  Touch File Python – How to Touch a File Using Python
  • 5.  Count Letters in Word in Python
  • 6.  Get Day of Year from Date in pandas DataFrame
  • 7.  Random Number Without Repeating in Python
  • 8.  Using Python to Calculate Average of List of Numbers
  • 9.  Python Get Yesterday’s Date
  • 10.  Solve Python Object Does Not Support Indexing Error

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy