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.

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠