Skip to content

Commit ea520e6

Browse files
author
Antti Myyrä
committed
Storages: do not load image to memory before uploading, remove zone default
1 parent 99a758f commit ea520e6

File tree

2 files changed

+20
-25
lines changed

2 files changed

+20
-25
lines changed

upcloud_api/cloud_manager/storage_mixin.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from typing import Optional, Union
2+
from pathlib import Path
23

34
from upcloud_api.api import API
45
from upcloud_api.storage import Storage
56
from upcloud_api.storage_import import StorageImport
6-
from upcloud_api.utils import get_raw_data_from_file
77

88

99
class StorageManager:
@@ -41,10 +41,10 @@ def get_storage(self, storage: str) -> Storage:
4141

4242
def create_storage(
4343
self,
44+
zone: str,
4445
size: int = 10,
4546
tier: str = 'maxiops',
4647
title: str = 'Storage disk',
47-
zone: str = 'fi-hel1',
4848
backup_rule: Optional[dict] = None,
4949
) -> Storage:
5050
"""
@@ -184,33 +184,37 @@ def create_storage_import(
184184
Creates an import task to import data into an existing storage.
185185
Source types: http_import or direct_upload.
186186
"""
187+
if source not in ["http_import", "direct_upload"]:
188+
raise Exception("invalid storage import source: %s", source)
189+
187190
url = f'/storage/{storage}/import'
188191
body = {'storage_import': {'source': source}}
189192
if source_location:
190193
body['storage_import']['source_location'] = source_location
191194
res = self.api.post_request(url, body)
192195
return StorageImport(**res['storage_import'])
193196

194-
def upload_file_for_storage_import(self, storage_import, file):
197+
def upload_file_for_storage_import(self, storage_import: StorageImport, file: Path, timeout: int = 600):
195198
"""
196199
Uploads a file directly to UpCloud's uploader session.
197200
"""
198-
# TODO: this should not buffer the entire `file` into memory
199-
200-
# This is importing and using `requests` directly since there doesn't
201-
# seem to be a point in adding a `.api.raw_request()` call to the `API` class.
202-
# That could be changed.
203201

204202
import requests
205203

206-
resp = requests.put(
207-
url=storage_import.direct_upload_url,
208-
data=get_raw_data_from_file(file),
209-
headers={'Content-type': 'application/octet-stream'},
210-
timeout=600,
211-
)
212-
resp.raise_for_status()
213-
return resp.json()
204+
# This is importing and using `requests` directly since there doesn't
205+
# seem to be a point in adding a `.api.raw_request()` call to the `API` class.
206+
# That could be changed if there starts to be more of these cases.
207+
208+
with open(file, 'rb') as f:
209+
resp = requests.put(
210+
url=storage_import.direct_upload_url,
211+
data=f,
212+
headers={'Content-type': 'application/octet-stream'},
213+
timeout=timeout,
214+
)
215+
216+
resp.raise_for_status()
217+
return resp.json()
214218

215219
def get_storage_import_details(self, storage: str) -> StorageImport:
216220
"""

upcloud_api/utils.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,3 @@ def try_it_n_times(operation, expected_error_codes, custom_error='operation fail
2323
if i >= n - 1:
2424
raise UpCloudClientError(custom_error)
2525

26-
27-
def get_raw_data_from_file(file: str) -> bytes:
28-
"""
29-
Helper function to get raw file data for uploading.
30-
"""
31-
with open(file, 'rb') as file:
32-
data = file.read()
33-
file.close()
34-
return data

0 commit comments

Comments
 (0)