捕捉S3异常

0

【以下的问题经过翻译处理】 我有一个使用S3资源的代码。客户端错误异常应该捕捉那个资源的异常,但如果我的网络连接中断,就不会被捕捉到。我如何知道我的代码设置了捕捉所有必须捕捉的异常?这听起来像一个基础问题,但我很难弄清楚。我看了文档,它说客户端错误会捕捉所有资源异常,但事实并非如此。谢谢帮助。

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

except botocore.exceptions.ClientError as err: print('\n In ClientError') print('\n source is %s' %(source)) print('\n Error Code: {}'。format(err.response ['Error'] ['Code'])) print('\n Error Message: {}'。format(err.response ['Error'] ['Message'])) print('\n Request ID: {}'。format(err.response ['ResponseMetadata'] ['RequestId'])) print('\n Http 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

profile picture
专家
已提问 5 个月前19 查看次数
1 回答
0

【以下的回答经过翻译处理】 如果你想提供更多关于ClientErrors中错误的细节,可以查看ResponseMetadata嵌套字典中出现的Error嵌套字典。需要记住的一点是,Boto3将所有AWS服务的错误和异常分类为ClientError异常。

但是,如果你正在使用资源客户端时寻找异常,可能会略有不同,因为捕获异常和错误与使用某些AWS服务的低级客户端具有类似的体验。对于这些,解析的错误响应可能看起来不同,因为你需要访问客户端的meta属性来获取异常,例如client.meta.client.exceptions.<insert-exception-here>

了解更多信息,请访问:https://boto3.amazonaws.com/v1/documentation/api/latest/guide/error-handling.html

profile picture
专家
已回答 5 个月前

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

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

回答问题的准则