定義介面實現
class ObjectAuth(ABC):
@staticmethod
def dispatch(type):
for sub_cls in ObjectAuth.__subclasses__():
if sub_cls.__name__.lower().find(type.lower()) >= 0:
return sub_cls
raise Exception("沒有合適的ObjectAuth類進行分配")
@abstractmethod
def gen_temp_access_key(self, bucket, resource):
raise NotImplementedError()
具體實現
class OOSAuth(ObjectAuth):
def __init__(self, remote_config) -> None:
self.read_only_ak = remote_config.get('read_only_ak')
self.read_only_sk = remote_config.get('read_only_sk')
def gen_temp_access_key(self, bucket, resource):
return self.read_only_ak, self.read_only_sk, ''
使用實現類方式
auth = ObjectAuth.dispatch(type)(file_uploader_config)
auth.gen_temp_access_key(bucket, resource)