异常处理
更新时间:2021-08-27
系统异常
BOS系统异常提示有如下三种方式:
异常方法 | 说明 |
---|---|
BceHttpClientError | 重试时抛出的异常 |
last_error | 最后一次重试时抛出的异常 |
BceClientError | BOS客户端产生的异常 |
BceInvalidArgumentError | 传递参数产生的异常 |
BceServerError | BOS服务器产生的异常 |
用户可以使用try获取某个事件所产生的异常:
from baidubce import exception
from baidubce.services import bos
try:
bos_client.delete_object(bucket_name, object_key)
except exception.BceHttpClientError as e:
print e.last_error
返回为:
BceHttpClientError: Unable to execute HTTP request. Retried 0 times. All trace backs:
Traceback (most recent call last):
File "/home/work/python-2.7/lib/python2.7/site-packages/baidubce/http/bce_http_client.py", line 184, in send_request
if handler_function(http_response, response):
File "/home/work/python-2.7/lib/python2.7/site-packages/baidubce/http/handler.py", line 71, in parse_error raise bse
BceServerError: The specified key does not exist.
客户端异常
客户端异常表示客户端尝试向BOS发送请求以及数据传输时遇到的异常。 例如,当发送请求时网络连接不可用时,则会抛出BceHttpClientError;当上传文件时发生IO异常时,也会抛出BceClientError。
服务端异常
当BOS服务端出现异常时,BOS服务端会返回给用户相应的错误信息,以便定位问题。常见服务端异常可参见BOS错误信息格式
参数异常
BOS Python SDK的每个调用都有一些类型固定不可以为空的参数,若该参数传入为空值则返回ValueError,若该参数传入类型错误则返回TypeError。
参数与调用的对应关系如下:
BOS调用 | 参数 | 说明 | 类型 |
---|---|---|---|
create_bucket, does_bucket_exist, delete_bucket, get_bucket_location, get_bucket_lifecycle, delete_bucket_lifecycle, get_bucket_cors, delete_bucket_cors, get_bucket_logging, delete_bucket_logging, list_objects, list_all_objects, delete_object, get_bucket_acl, list_multipart_uploads, list_all_multipart_uploads | bucket_name | Bucket名称 | string, unicode |
put_bucket_logging | source_bucket,target_bucket | 源Bucket和目标Bucket名称 | string, unicode |
set_bucket_acl | bucket_name | Bucket名称 | string, unicode |
acl | ACL主体包含被授权人及权限 | list, dict | |
set_bucket_canned_acl | bucket_name | Bucket名称 | string, unicode |
canned_acl | CannedAcl权限 | string | |
put_bucket_lifecycle | bucket_name | Bucket名称 | string, unicode |
rules | 生命周期管理规则主体,包含资源及操作动作 | list, dict | |
put_bucket_cors | bucket_name | Bucket名称 | string, unicode |
cors_configuration | 跨域资源共享(CORS)的规则主体,包含允许的跨域请求的来源及允许的header等 | list, dict | |
get_object, get_object_as_string, get_object_meta_data, initiate_multipart_upload, generate_pre_signed_url | bucket_name | Bucket名称 | string, unicode |
key | Object名称 | string | |
get_object_to_file, put_object_from_file | bucket_name | Bucket名称 | string, unicode |
key | Object名称 | string | |
file_name | 文件名 | string | |
put_object, append_object | bucket_name | Bucket名称 | string, unicode |
key | Object名称 | string | |
data | 流对象 | object | |
content_length | 上传Object的大小 | int, long | |
content_md5 | 上传Object的MD5 | string | |
put_object_from_string,append_object_from_string | bucket_name | Bucket名称 | string, unicode |
key | Object名称 | string | |
data | 上传字符串 | string, unicode | |
copy_object | source_bucket_name, target_bucket_name | 源Bucket和目标Bucket名称 | string, unicode |
source_key, target_key | 源Object和目标Object名称 | string | |
upload_part | bucket_name, upload_id | Bucket名称,标识MultUpload操作全局ID | string, unicode |
key | Object名称 | string | |
part_number | 分块编号 | int | |
part_size | 上传分块的大小 | int, long | |
part_fp | 上传分块对象 | object | |
upload_part_from_file | bucket_name, upload_id | Bucket名称,标识MultUpload操作全局ID | string, unicode |
key | Object名称 | string | |
part_number | 分块编号 | int | |
part_size | 上传分块的大小 | int, long | |
file_name | 文件名 | string | |
offset | 分块的开始偏移位置 | int | |
complete_multipart_upload | bucket_name, upload_id | Bucket名称,标识MultUpload操作全局ID | string, unicode |
key | Object名称 | string | |
part_list | 分块列表 | list | |
abort_multipart_upload, list_parts, list_all_parts | bucket_name, upload_id | Bucket名称,标识MultUpload操作全局ID | string, unicode |
key | Object名称 | string | |
delete_multiple_objects | bucket_name | Bucket名称 | string, unicode |
key_list | object列表 | list |