Skip to content

Commit

Permalink
Check conf in objwrapper (#86)
Browse files Browse the repository at this point in the history
Co-authored-by: 杨赫然 <heran.yang@seafile.com>
  • Loading branch information
feiniks and 杨赫然 authored Oct 30, 2024
1 parent 515fe14 commit e971c15
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 17 deletions.
10 changes: 8 additions & 2 deletions objwrapper/alioss.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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):
Expand Down
16 changes: 16 additions & 0 deletions objwrapper/exceptions.py
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions objwrapper/s3.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
20 changes: 6 additions & 14 deletions seafobj/objstore_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'):
Expand Down Expand Up @@ -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:
Expand All @@ -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

Expand All @@ -158,19 +152,17 @@ 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:
if str(cfg['use_https']).lower().strip() == 'true':
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

Expand Down
2 changes: 1 addition & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit e971c15

Please sign in to comment.