Skip to content

Commit e428145

Browse files
fix upload
1 parent 9f4bbd8 commit e428145

File tree

5 files changed

+50
-36
lines changed

5 files changed

+50
-36
lines changed

pkg/sumdb/sum.golang.org/latest

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
go.sum database tree
2+
39073249
3+
CHoGLc3xiTLWJIkUvqwtoUC8uUi8tsi/AMKPaA1nDxo=
4+
5+
— sum.golang.org Az3grieYv97xelotCMbg3+3zXebCIZkZfor40fTCTWo4rNjv+mQ8eEJFgiyyjPkL1h6GWziyoBzovPY6pFcfbssUNQU=

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def read_file(filename):
1111

1212
setup(
1313
name="bohrium-sdk",
14-
version="0.8.0",
14+
version="0.10.0",
1515
author="dingzhaohan",
1616
author_email="[email protected]",
1717
url="https://github.com/dingzhaohan",

src/bohrium/_base_client.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ def _build_headers(self, custom_headers) -> httpx.Headers:
143143
headers_dict = _merge_mappings(
144144
self.default_headers, self._custom_headers, custom_headers
145145
)
146+
# 过滤掉 value 为 None 的 header
147+
headers_dict = {k: v for k, v in headers_dict.items() if v is not None}
146148
headers = httpx.Headers(headers_dict)
147149
return headers or dict()
148150

@@ -177,7 +179,7 @@ def platform_headers(self) -> Dict[str, str]:
177179
exceptions=(httpx.RequestError,),
178180
)
179181
def _request(
180-
self, method: str, path: str, json=None, headers=None, **kwargs
182+
self, method: str, path: str, json=None, headers=None, data=None, **kwargs
181183
) -> httpx.Response:
182184
url = urljoin(str(self._base_url), path)
183185
logger.info(f"Requesting {method} {url}")
@@ -188,6 +190,7 @@ def _request(
188190
method.upper(),
189191
url,
190192
json=json,
193+
data=data,
191194
headers=merged_headers,
192195
params=merged_params,
193196
)

src/bohrium/resources/job/job.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ def submit(
5858
):
5959
# log.info(f"submit job {name},project_id:{project_id}")
6060
data = self.create_job(project_id, job_name, job_group_id)
61-
print(data)
6261
if work_dir != "":
6362
if not os.path.exists(work_dir):
6463
raise FileNotFoundError
@@ -97,36 +96,30 @@ def insert(self, **kwargs):
9796
if 'logFiles' in camel_data and not isinstance(camel_data['logFiles'], list):
9897
camel_data['logFiles'] = [camel_data['logFiles']]
9998
response = self._client.post("/openapi/v2/job/add", json=camel_data)
100-
pprint(response.request)
101-
print(response.json())
10299
return response.json().get("data")
103100

104101
def delete(self, job_id):
105102
# log.info(f"delete job {job_id}")
106103
response = self._client.post(f"/openapi/v1/job/del/{job_id}")
107-
pprint(response.request)
108-
print(response.json())
104+
109105

110106
def terminate(self, job_id):
111107
# log.info(f"terminate job {job_id}")
112108
response = self._client.post(f"/openapi/v1/job/terminate/{job_id}")
113-
pprint(response.request)
114-
print(response.json())
109+
115110

116111
def kill(self, job_id):
117112
# log.info(f"kill job {job_id}")
118113
response = self._client.post(f"/openapi/v1/job/kill/{job_id}")
119-
pprint(response.request)
120-
print(response.json())
114+
121115

122116
def log(self, job_id, log_file="STDOUTERR", page=-1, page_size=8192):
123117
# log.info(f"log job {job_id}")
124118
response = self._client.get(
125119
f"/openapi/v1/job/{job_id}/log",
126120
params={"logFile": log_file, "page": page, "pageSize": page_size},
127121
)
128-
pprint(response.request)
129-
print(response.json().get("data")["log"])
122+
130123
return response.json().get("data")["log"]
131124

132125
def create_job(
@@ -154,8 +147,7 @@ def create_job(
154147
"bohrGroupId": group_id,
155148
}
156149
response = self._client.post(f"/openapi/v1/job/create", json=data)
157-
pprint(response.request)
158-
print(response.json())
150+
159151
return response.json().get("data")
160152

161153
def create_job_group(self, project_id, job_group_name):
@@ -164,9 +156,8 @@ def create_job_group(self, project_id, job_group_name):
164156
"/openapi/v1/job_group/add",
165157
json={"name": job_group_name, "projectId": project_id},
166158
)
167-
pprint(response.request)
168-
print(response.json())
169-
159+
return response.json().get("data")
160+
170161
def upload(
171162
self,
172163
file_path: str,

src/bohrium/resources/tiefblue/tiefblue.py

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def write(
7171
}
7272
headers[self.TIEFBLUE_HEADER_KEY] = self.encode_base64(param)
7373
req = self.client.post("/api/upload/binary", headers=headers, data=data)
74-
pprint(req.request)
74+
7575
return req
7676

7777
def read(
@@ -103,40 +103,48 @@ def upload_from_file(
103103
parameter = Parameter()
104104
parameter.contentDisposition = f'attachment; filename="{disposition}"'
105105
with open(file_path, 'rb') as fp:
106-
res = self.write(object_key=object_key, data=fp.read(), parameter=parameter)
106+
res = self.write(object_key=object_key, data=fp.read(), parameter=parameter, token=token)
107107
return res
108108

109-
def init_upload_by_part(self, object_key: str, parameter=None):
109+
def init_upload_by_part(self, object_key: str, parameter=None, token: str = ""):
110110
data = {
111111
'path': object_key
112112
}
113113
if parameter is not None:
114114
data['option'] = parameter.__dict__
115+
headers = {
116+
"Authorization": f"Bearer {token}",
117+
}
115118
url = f"/api/upload/multipart/init"
116-
return self.client.post(url, host=self.host, json=data)
119+
return self.client.post(url, host=self.host, headers=headers, json=data)
117120

118-
def upload_by_part(self, object_key: str, initial_key: str, chunk_size: int, number: int, body):
121+
def upload_by_part(self, object_key: str, initial_key: str, chunk_size: int, number: int, body, token: str = ""):
119122
param = {
120123
'initialKey': initial_key,
121124
'number': number,
122125
'partSize': chunk_size,
123126
'objectKey': object_key
124127
}
125-
headers = {}
128+
headers = {
129+
"Authorization": f"Bearer {token}",
130+
}
126131
headers[self.TIEFBLUE_HEADER_KEY] = self._dump_parameter(param)
127132
url = f"/api/upload/multipart/upload"
128133
resp = self.client.post(url, host=self.host, data=body, headers=headers)
129134
return resp
130135

131136

132-
def complete_upload_by_part(self, object_key, initial_key, part_string):
137+
def complete_upload_by_part(self, object_key, initial_key, part_string, token):
133138
data = {
134139
'path': object_key,
135140
'initialKey': initial_key,
136141
'partString': part_string
137142
}
143+
headers = {
144+
"Authorization": f"Bearer {token}",
145+
}
138146
url = f"/api/upload/multipart/complete"
139-
resp = self.client.post(url, host=self.host, json=data)
147+
resp = self.client.post(url, host=self.host, headers=headers, json=data)
140148
return resp
141149

142150
def upload_From_file_multi_part(
@@ -161,27 +169,34 @@ def upload_From_file_multi_part(
161169
parameter = Parameter()
162170
parameter.contentDisposition = f'attachment; filename="{disposition}"'
163171
bar_format = "{l_bar}{bar}| {n:.02f}/{total:.02f} % [{elapsed}<{remaining}, {rate_fmt}{postfix}]"
164-
with open(file_path, 'r') as f:
172+
with open(file_path, 'rb') as f:
165173
pbar = tqdm(total=100, desc=f"Uploading {disposition}", smoothing=0.01, bar_format=bar_format,
166174
disable=not progress_bar)
167-
f.seek(0)
168175
if size < _DEFAULT_CHUNK_SIZE * 2:
169-
self.write(object_key=object_key, data=f.buffer, parameter=parameter, token=token)
176+
177+
self.write(object_key=object_key, data=f.read(), parameter=parameter, token=token)
170178
pbar.update(100)
171179
pbar.close()
172180
return
173181
chunks = split_size_by_part_size(size, chunk_size)
174-
initial_key = self.init_upload_by_part(object_key, parameter).get('initialKey')
182+
initial_key = self.init_upload_by_part(object_key, parameter, token).get('initialKey')
175183
part_string = []
184+
uploaded = 0
176185
for c in chunks:
177186
f.seek(c.Offset)
178-
num_to_upload = min(chunk_size, size - c.Offset)
179-
part_string.append(self.upload_by_part(object_key, initial_key, chunk_size=c.Size, number=c.Number,
180-
body=f.buffer.read(c.Size)).get('partString'))
181-
percent = num_to_upload * 100 / (size + 1)
182-
pbar.update(percent)
187+
chunk_data = f.read(c.Size)
188+
resp = self.upload_by_part(
189+
object_key, initial_key, chunk_size=c.Size, number=c.Number,
190+
body=chunk_data, token=token
191+
)
192+
part_string.append(resp.get('partString'))
193+
uploaded += c.Size
194+
percent = uploaded * 100 / size
195+
pbar.n = percent
196+
pbar.refresh()
197+
pbar.update(100 - pbar.n)
183198
pbar.close()
184-
return self.complete_upload_by_part(object_key, initial_key, part_string)
199+
return self.complete_upload_by_part(object_key, initial_key, part_string, token)
185200

186201
def download_from_file(self):
187202

0 commit comments

Comments
 (0)