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

已提问 2 年前786 查看次数
1 回答
0
已接受的回答

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
已回答 2 年前
  • 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.

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则

相关内容