Spaces:
Sleeping
Sleeping
import os | |
import traceback | |
from obs import ObsClient | |
from obs import PutObjectHeader | |
AK = os.environ["OBS_AK"] | |
SK = os.environ["OBS_SK"] | |
ENDPOINT = os.environ["OBS_ENDPOINT"] | |
BUCKET_NAME = os.environ["BUCKET"] | |
class OBSHandler: | |
def __init__(self): | |
self.access_key = AK | |
self.secret_key = SK | |
self.endpoint = ENDPOINT | |
self.bucket_name = BUCKET_NAME | |
self.server = "https://" + self.endpoint | |
self.obsClient = self.init_obs() | |
self.maxkeys = 1000 # 查询的对象最大个数 | |
# 初始化obs | |
def init_obs(self): | |
obsClient = ObsClient(access_key_id=self.access_key, | |
secret_access_key=self.secret_key, | |
server=self.server) | |
return obsClient | |
def close_obs(self): | |
self.obsClient.close() | |
def upload_file(self, dest, source): | |
response_msg = {'status': 200, 'msg': '单个对象上传成功'} | |
headers = PutObjectHeader() | |
headers.contentType = 'image/jpeg' | |
try: | |
resp = self.obsClient.putFile(self.bucket_name, | |
dest, | |
source, | |
headers=headers) | |
if resp.status < 300: | |
print("Upload image to obs successfully!") | |
else: | |
print('errorCode:', resp.errorCode) | |
print('errorMessage:', resp.errorMessage) | |
except Exception: | |
print(traceback.format_exc()) | |
response_msg["status"] = -1 | |
response_msg["msg"] = "单个对象上传失败" | |
print("Upload image to obs error!") | |
return response_msg |