From 5afcd5ddc478181e5fba4075a4e4981418692a69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E8=B5=AB=E7=84=B6?= Date: Wed, 30 Oct 2024 11:29:26 +0800 Subject: [PATCH] Check conf in objwrapper --- objwrapper/alioss.py | 10 ++++++++-- objwrapper/exceptions.py | 16 ++++++++++++++++ objwrapper/s3.py | 3 +++ seafobj/objstore_factory.py | 20 ++++++-------------- tests/utils.py | 2 +- 5 files changed, 34 insertions(+), 17 deletions(-) create mode 100644 objwrapper/exceptions.py diff --git a/objwrapper/alioss.py b/objwrapper/alioss.py index 8409d33..810d555 100644 --- a/objwrapper/alioss.py +++ b/objwrapper/alioss.py @@ -1,5 +1,6 @@ import http.client import oss2 +from objwrapper.exceptions import InvalidConfigError # set log level to WARNING # the api set_file_logger exists after oss2 2.6.0, which has a lot of 'INFO' log @@ -10,11 +11,16 @@ pass class OSSConf(object): - def __init__(self, key_id, key, bucket_name, host, use_https): + def __init__(self, key_id, key, bucket_name, host, region, use_https): + if not host and not region: + raise InvalidConfigError('endpoint and region are not configured') + self.host = host + if not host: + self.host = 'oss-cn-%s-internal.aliyuncs.com' % region self.key_id = key_id self.key = key self.bucket_name = bucket_name - self.host = host + self.region = region self.use_https = use_https class SeafOSSClient(object): diff --git a/objwrapper/exceptions.py b/objwrapper/exceptions.py new file mode 100644 index 0000000..1f0ae0f --- /dev/null +++ b/objwrapper/exceptions.py @@ -0,0 +1,16 @@ +#coding: UTF-8 + +class ObjWrapperException(Exception): + def __init__(self, msg): + Exception.__init__(self) + self.msg = str(msg) + + def __str__(self): + return self.msg + +class InvalidConfigError(ObjWrapperException): + '''This Exception is rasied when error happens during parsing + seafile.conf + + ''' + pass diff --git a/objwrapper/s3.py b/objwrapper/s3.py index f7891d0..eb0a626 100644 --- a/objwrapper/s3.py +++ b/objwrapper/s3.py @@ -1,8 +1,11 @@ import boto3 from botocore.exceptions import ClientError +from objwrapper.exceptions import InvalidConfigError class S3Conf(object): def __init__(self, key_id, key, bucket_name, host, port, use_v4_sig, aws_region, use_https, path_style_request, sse_c_key): + if not host and not aws_region: + raise InvalidConfigError('aws_region and host are not configured') self.key_id = key_id self.key = key self.bucket_name = bucket_name diff --git a/seafobj/objstore_factory.py b/seafobj/objstore_factory.py index de70d78..75be544 100644 --- a/seafobj/objstore_factory.py +++ b/seafobj/objstore_factory.py @@ -60,8 +60,6 @@ def get_s3_conf(cfg, section): aws_region = None if cfg.has_option(section, 'aws_region'): aws_region = cfg.get(section, 'aws_region') - if not host and not aws_region: - raise InvalidConfigError('aws_region and host are not configured') use_https = False if cfg.has_option(section, 'use_https'): @@ -106,8 +104,6 @@ def get_s3_conf_from_json(cfg): aws_region = None if 'aws_region' in cfg: aws_region = cfg['aws_region'] - if not host and not aws_region: - raise InvalidConfigError('aws_region and host are not configured') use_https = False if 'use_https' in cfg: @@ -134,18 +130,16 @@ def get_oss_conf(cfg, section): endpoint = '' if cfg.has_option(section, 'endpoint'): endpoint = cfg.get(section, 'endpoint') - if not endpoint: + region = '' + if cfg.has_option(section, 'region'): region = cfg.get(section, 'region') - endpoint = 'oss-cn-%s-internal.aliyuncs.com' % region - - host = endpoint use_https = False if cfg.has_option(section, 'use_https'): use_https = cfg.getboolean(section, 'use_https') from objwrapper.alioss import OSSConf - conf = OSSConf(key_id, key, bucket, host, use_https) + conf = OSSConf(key_id, key, bucket, endpoint, region, use_https) return conf @@ -158,11 +152,9 @@ def get_oss_conf_from_json(cfg): if 'endpoint' in cfg: endpoint = cfg['endpoint'] - if not endpoint: + region = '' + if 'region' in cfg: region = cfg['region'] - endpoint = 'oss-cn-%s-internal.aliyuncs.com' % region - - host = endpoint use_https = False if 'use_https' in cfg: @@ -170,7 +162,7 @@ def get_oss_conf_from_json(cfg): use_https = True from objwrapper.alioss import OSSConf - conf = OSSConf(key_id, key, bucket, host, use_https) + conf = OSSConf(key_id, key, bucket, endpoint, region, use_https) return conf diff --git a/tests/utils.py b/tests/utils.py index 7ee279d..b2173ad 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -23,6 +23,6 @@ def get_s3_client(sse_c_key): def get_oss_client(): host = f'oss-{oss_region}.aliyuncs.com' - conf = OSSConf(oss_key_id, oss_key, oss_bucket, host, True) + conf = OSSConf(oss_key_id, oss_key, oss_bucket, host, oss_region, True) client = SeafOSSClient(conf) return client