Spaces:
Sleeping
Sleeping
Delete obshandler.py
Browse files- obshandler.py +0 -54
obshandler.py
DELETED
@@ -1,54 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import traceback
|
3 |
-
|
4 |
-
from obs import ObsClient
|
5 |
-
from obs import PutObjectHeader
|
6 |
-
|
7 |
-
AK = os.environ["OBS_AK"]
|
8 |
-
SK = os.environ["OBS_SK"]
|
9 |
-
|
10 |
-
ENDPOINT = os.environ["OBS_ENDPOINT"]
|
11 |
-
BUCKET_NAME = os.environ["BUCKET"]
|
12 |
-
|
13 |
-
|
14 |
-
class OBSHandler:
|
15 |
-
def __init__(self):
|
16 |
-
self.access_key = AK
|
17 |
-
self.secret_key = SK
|
18 |
-
self.endpoint = ENDPOINT
|
19 |
-
self.bucket_name = BUCKET_NAME
|
20 |
-
self.server = "https://" + self.endpoint
|
21 |
-
self.obsClient = self.init_obs()
|
22 |
-
self.maxkeys = 1000 # 查询的对象最大个数
|
23 |
-
|
24 |
-
# 初始化obs
|
25 |
-
def init_obs(self):
|
26 |
-
obsClient = ObsClient(access_key_id=self.access_key,
|
27 |
-
secret_access_key=self.secret_key,
|
28 |
-
server=self.server)
|
29 |
-
return obsClient
|
30 |
-
|
31 |
-
def close_obs(self):
|
32 |
-
self.obsClient.close()
|
33 |
-
|
34 |
-
def upload_file(self, dest, source):
|
35 |
-
response_msg = {'status': 200, 'msg': '单个对象上传成功'}
|
36 |
-
headers = PutObjectHeader()
|
37 |
-
headers.contentType = 'image/jpeg'
|
38 |
-
try:
|
39 |
-
resp = self.obsClient.putFile(self.bucket_name,
|
40 |
-
dest,
|
41 |
-
source,
|
42 |
-
headers=headers)
|
43 |
-
|
44 |
-
if resp.status < 300:
|
45 |
-
print("Upload image to obs successfully!")
|
46 |
-
else:
|
47 |
-
print('errorCode:', resp.errorCode)
|
48 |
-
print('errorMessage:', resp.errorMessage)
|
49 |
-
except Exception:
|
50 |
-
print(traceback.format_exc())
|
51 |
-
response_msg["status"] = -1
|
52 |
-
response_msg["msg"] = "单个对象上传失败"
|
53 |
-
print("Upload image to obs error!")
|
54 |
-
return response_msg
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|