File size: 1,593 Bytes
8bea69a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/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