BACKGROUND
I have a function where i cache file from aws s3. I provide it the path on s3 and also the local path of where i want the cached file to live. Sometimes if the file does not exist on s3 it will return a 404 error. That makes sense but i want to handle it gracefully. What i want to do is if the file i am trying to cache does not exist I want my get_overview_stream function to return None.
CODE
def get_overview_stream(*, environment, proxy_key):
s3_overview_file_path = f"s3://{FOO_TRACK_BUCKET}/{environment}"
overview_file = f"{proxy_key}.csv"
local_path = cache_directory(environment)
try:
cache.cache_file(s3_overview_file_path, local_path, overview_file)
overview_file_cache = local_path / f"{proxy_key}.csv"
return overview_file_cache.open("r")
except botocore.exceptions.ClientError as e:
if e.response["Error"]["Code"] == "404":
return None
else:
raise
Attempted Solution Issue
I am very new to python so i am not sure if this the best way to handle this exception and if there is a more cleaner way. If so i would love to hear feedback.