47 lines
1.6 KiB
Python
47 lines
1.6 KiB
Python
from obs import ObsClient
|
|
import obs
|
|
from utils.config import cfg
|
|
from qcloud_cos import CosConfig,CosS3Client
|
|
from qcloud_cos.cos_exception import CosClientError, CosServiceError
|
|
|
|
def obsInit():
|
|
print('start init obs <<')
|
|
obsClient = ObsClient(
|
|
access_key_id=cfg.obs_access_key_id,
|
|
secret_access_key=cfg.obs_secret_access_key,
|
|
server=cfg.obs_server)
|
|
bucketName = cfg.obs_bucketName
|
|
headers = obs.SetObjectMetadataHeader()
|
|
headers.cacheControl = "no-cache"
|
|
return obsClient, headers
|
|
|
|
def cosInit():
|
|
print('start init cos <<')
|
|
secret_id = cfg.cos_secret_id
|
|
secret_key = cfg.cos_secret_key
|
|
region = cfg.cos_region
|
|
config = CosConfig(Region=region, Secret_id=secret_id, Secret_key=secret_key)
|
|
client = CosS3Client(config)
|
|
return client
|
|
|
|
class up_load:
|
|
def __init__(self):
|
|
self.cosclient = cosInit()
|
|
self.obsclient, self.headers = obsInit()
|
|
|
|
def upLoad(self, key=None, datapath=None):
|
|
# 使用高级接口断点续传,失败重试时不会上传已成功的分块(这里重试10次)
|
|
if cfg.cos:
|
|
for i in range(0, 10):
|
|
try:
|
|
response = self.cosclient.upload_file(
|
|
Bucket=cfg.cos_Bucket,
|
|
Key=key,
|
|
LocalFilePath=datapath)
|
|
break
|
|
except CosClientError or CosServiceError as e:
|
|
print(e)
|
|
if cfg.obs:
|
|
response = self.obsclient.putFile(cfg.obs_bucketName, key, datapath)
|
|
return response
|