catching S3 exceptions

0

I have code like this using the S3 resource. The client error exception is supposed to catch exceptions from that resource, but if my internet connection dies, it's not caught. How do I know my code is set up to catch all the exceptions I need to catch? This sounds like a basic question but I'm having a hard time figuring it out. I look at the documentation and it says a client error will catch all the resource exceptions, but it doesn't. Thanks for the help.

try: upload_file_response = s3.upload_file(source, destination, key, ExtraArgs=extra_args, Callback=callback, Config=config)

except botocore.exceptions.ClientError as err: print('\nIn ClientError') print('\n source is %s' % (source)) print('\nError Code: {}'.format(err.response['Error']['Code'])) print('\nError Message: {}'.format(err.response['Error']['Message'])) print('\nRequest ID: {}'.format(err.response['ResponseMetadata']['RequestId'])) print('\nHttp code: {}'.format(err.response['ResponseMetadata']['HTTPStatusCode'])) end_tstamp = datetime.now() print('End time is ', end_tstamp) duration = end_tstamp - tstamp print('Duration is ',str(duration).split('.')[0]) raise err

demandé il y a 2 ans786 vues
1 réponse
0
Réponse acceptée

If you're looking to provide more details on the errors in ClientErrors, you can check the Error nested dictionary that appears with the ResponseMetadata nested dictionary. One thing to keep in mind is that Boto3 classifies all AWS service errors and exceptions as ClientError exceptions as well.

However, if you're looking for exceptions while using a resource client, that may be slightly different as catching exceptions and errors is a similar experience to using a low-level client for certain AWS services. For these, parsing may look different for error responses as you’ll need to access the client’s meta property to get to the exceptions such as client.meta.client.exceptions.<insert-exception-here>.

For more information, check out: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/error-handling.html

jsonc
répondu il y a 2 ans
  • Maybe I should ask this. I have a client set up like this: s3 = boto3.client('s3').

    Why doesn't an "except botocore.exceptions.ClientError as err" statement catch botocore.exceptions.EndpointConnectionError errors or botocore.exceptions.ConnectionClosedError errors? Shouldn't they be caught? they are from the service, right?

  • Nevermind. I found what I was looking for in your response. The botocore exceptions are listed here. https://github.com/boto/botocore/blob/develop/botocore/exceptions.py Thanks.

Vous n'êtes pas connecté. Se connecter pour publier une réponse.

Une bonne réponse répond clairement à la question, contient des commentaires constructifs et encourage le développement professionnel de la personne qui pose la question.

Instructions pour répondre aux questions