1

clients.py

"""
Wraps boto3 to provide client and resource objects that connect to Localstack when
running tests if LOCALSTACK_URL env variable is set, otherwise connect to real AWS.
"""
import os

import boto3

# Port 4566 is the universal "edge" port for all services since localstack v0.11.0,
# so LOCALSTACK_URL can be of the form "http://hostname:4566".
# "None" means boto3 will use the default endpoint for the real AWS services.
ENDPOINT_URL = os.getenv("LOCALSTACK_URL", None)


def s3_resource():
    return boto3.resource("s3", endpoint_url=ENDPOINT_URL)


def s3_client():
    return boto3.client("s3", endpoint_url=ENDPOINT_URL)

mycode.py

from mymod.aws.clients import s3_resource
   s3 = s3_resource()


   def __get_object_etag(self, s3_dir_to_download_file_from: str, file_name: str) -> str
            bucket, key = s3.deconstruct_s3_path(
                f"{s3_dir_to_download_file_from}/{file_name}"
            )
            try:
                etag_value = s3_resource().Object(bucket, key).e_tag
                return etag_value
            except botocore.exceptions.ClientError:
                raise

I am wondering if in mycode.py I am doing the correct error handling when the file i am looking for does not exist on s3. I basically expect key to be there and if not i want to raise and error i do not want to proceed further as this code will be used part of a pipeline that relies on each previous step.

I would love to know if i am handling the error correct or if i am wrong then how would one handle this case?

From what I understand "exceptions" are suppose to handle errors and proceed further but in my case I do not want to proceed further in the rest of my code if the file i am looking for on S3 is missing.

Dinero
  • 1,070
  • 2
  • 19
  • 44
  • I think this discussion might be helpful: https://stackoverflow.com/questions/33842944/check-if-a-key-exists-in-a-bucket-in-s3-using-boto3 – Gevorg Davoian Jul 24 '20 at 12:51
  • right so my question is if i handle it as an exception will the rest of my code continue to exit ? @GevorgDavoian – Dinero Jul 24 '20 at 12:54
  • As far as I understand, you don't want to handle that exception and you're simply re-raising it. IMHO, you can omit the try-except block altogether (it's your assumption that the key does exist so you can't proceed otherwise). And yes, any unhandled exception will propagate and cause your program to abort its execution. – Gevorg Davoian Jul 24 '20 at 13:00

0 Answers0