Spaces:
Running
Running
#!/usr/bin/python3 | |
# -*- coding: utf-8 -*- | |
from toolbox.obs.tencent_cos import TencentCos | |
from toolbox.obs.aliyun_oss import AliyunOSS | |
class OBSManager(object): | |
def __init__(self, | |
obs_supplier: str, | |
url_prefix: str, | |
endpoint_url: str, | |
region: str, | |
secret_id: str, | |
secret_key: str, | |
bucket: str, | |
debug: bool = False | |
): | |
self.obs_supplier = obs_supplier | |
self.url_prefix = url_prefix | |
self.endpoint_url = endpoint_url | |
self.region = region | |
self.secret_id = secret_id | |
self.secret_key = secret_key | |
self.bucket = bucket | |
self.debug = debug | |
if obs_supplier == "tencent": | |
obs_cls = TencentCos | |
elif obs_supplier == "aliyun": | |
obs_cls = AliyunOSS | |
else: | |
raise AssertionError | |
self.obs_client = obs_cls( | |
endpoint_url=endpoint_url, | |
region=region, | |
secret_key=secret_key, | |
secret_id=secret_id, | |
bucket=bucket, | |
) | |
async def upload_file_to_obs(self, filename: str, url_path: str): | |
with open(filename, "rb") as f: | |
data_bytes = f.read() | |
cos_file_url = f"{self.url_prefix}/{url_path}" | |
if not self.debug: | |
result = await self.obs_client.upload_by_bytes( | |
data_bytes=data_bytes, | |
url_path=url_path, | |
) | |
return cos_file_url | |
if __name__ == "__main__": | |
pass | |