Skip to content

Commit e971c15

Browse files
feiniks杨赫然
andauthored
Check conf in objwrapper (#86)
Co-authored-by: 杨赫然 <[email protected]>
1 parent 515fe14 commit e971c15

File tree

5 files changed

+34
-17
lines changed

5 files changed

+34
-17
lines changed

objwrapper/alioss.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import http.client
22
import oss2
3+
from objwrapper.exceptions import InvalidConfigError
34

45
# set log level to WARNING
56
# the api set_file_logger exists after oss2 2.6.0, which has a lot of 'INFO' log
@@ -10,11 +11,16 @@
1011
pass
1112

1213
class OSSConf(object):
13-
def __init__(self, key_id, key, bucket_name, host, use_https):
14+
def __init__(self, key_id, key, bucket_name, host, region, use_https):
15+
if not host and not region:
16+
raise InvalidConfigError('endpoint and region are not configured')
17+
self.host = host
18+
if not host:
19+
self.host = 'oss-cn-%s-internal.aliyuncs.com' % region
1420
self.key_id = key_id
1521
self.key = key
1622
self.bucket_name = bucket_name
17-
self.host = host
23+
self.region = region
1824
self.use_https = use_https
1925

2026
class SeafOSSClient(object):

objwrapper/exceptions.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#coding: UTF-8
2+
3+
class ObjWrapperException(Exception):
4+
def __init__(self, msg):
5+
Exception.__init__(self)
6+
self.msg = str(msg)
7+
8+
def __str__(self):
9+
return self.msg
10+
11+
class InvalidConfigError(ObjWrapperException):
12+
'''This Exception is rasied when error happens during parsing
13+
seafile.conf
14+
15+
'''
16+
pass

objwrapper/s3.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import boto3
22
from botocore.exceptions import ClientError
3+
from objwrapper.exceptions import InvalidConfigError
34

45
class S3Conf(object):
56
def __init__(self, key_id, key, bucket_name, host, port, use_v4_sig, aws_region, use_https, path_style_request, sse_c_key):
7+
if not host and not aws_region:
8+
raise InvalidConfigError('aws_region and host are not configured')
69
self.key_id = key_id
710
self.key = key
811
self.bucket_name = bucket_name

seafobj/objstore_factory.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ def get_s3_conf(cfg, section):
6060
aws_region = None
6161
if cfg.has_option(section, 'aws_region'):
6262
aws_region = cfg.get(section, 'aws_region')
63-
if not host and not aws_region:
64-
raise InvalidConfigError('aws_region and host are not configured')
6563

6664
use_https = False
6765
if cfg.has_option(section, 'use_https'):
@@ -106,8 +104,6 @@ def get_s3_conf_from_json(cfg):
106104
aws_region = None
107105
if 'aws_region' in cfg:
108106
aws_region = cfg['aws_region']
109-
if not host and not aws_region:
110-
raise InvalidConfigError('aws_region and host are not configured')
111107

112108
use_https = False
113109
if 'use_https' in cfg:
@@ -134,18 +130,16 @@ def get_oss_conf(cfg, section):
134130
endpoint = ''
135131
if cfg.has_option(section, 'endpoint'):
136132
endpoint = cfg.get(section, 'endpoint')
137-
if not endpoint:
133+
region = ''
134+
if cfg.has_option(section, 'region'):
138135
region = cfg.get(section, 'region')
139-
endpoint = 'oss-cn-%s-internal.aliyuncs.com' % region
140-
141-
host = endpoint
142136

143137
use_https = False
144138
if cfg.has_option(section, 'use_https'):
145139
use_https = cfg.getboolean(section, 'use_https')
146140

147141
from objwrapper.alioss import OSSConf
148-
conf = OSSConf(key_id, key, bucket, host, use_https)
142+
conf = OSSConf(key_id, key, bucket, endpoint, region, use_https)
149143

150144
return conf
151145

@@ -158,19 +152,17 @@ def get_oss_conf_from_json(cfg):
158152

159153
if 'endpoint' in cfg:
160154
endpoint = cfg['endpoint']
161-
if not endpoint:
155+
region = ''
156+
if 'region' in cfg:
162157
region = cfg['region']
163-
endpoint = 'oss-cn-%s-internal.aliyuncs.com' % region
164-
165-
host = endpoint
166158

167159
use_https = False
168160
if 'use_https' in cfg:
169161
if str(cfg['use_https']).lower().strip() == 'true':
170162
use_https = True
171163

172164
from objwrapper.alioss import OSSConf
173-
conf = OSSConf(key_id, key, bucket, host, use_https)
165+
conf = OSSConf(key_id, key, bucket, endpoint, region, use_https)
174166

175167
return conf
176168

tests/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ def get_s3_client(sse_c_key):
2323

2424
def get_oss_client():
2525
host = f'oss-{oss_region}.aliyuncs.com'
26-
conf = OSSConf(oss_key_id, oss_key, oss_bucket, host, True)
26+
conf = OSSConf(oss_key_id, oss_key, oss_bucket, host, oss_region, True)
2727
client = SeafOSSClient(conf)
2828
return client

0 commit comments

Comments
 (0)