Skip to content

Commit 17309b4

Browse files
author
Antti Myyrä
committed
Storage upload: support paths and strings in filepaths, allow changing content type
1 parent 4d87197 commit 17309b4

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

docs/Storage.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ storage.destroy()
8888
Storages can be imported either by passing a URL or by uploading the file. Currently .iso, .raw and .img formats
8989
are supported. Other formats like qcow2 or vmdk should be converted before uploading.
9090

91+
Uploaded storage is expected to be uncompressed. It is possible to upload zip (`application/gzip`)
92+
or gzip (`application/x-xz`) compressed files, but you need to specify a separate `content_type`
93+
when calling the `upload_file_for_storage_import` function.
94+
9195
Warning: size of the import cannot exceed the size of the storage. The data will be written starting from
9296
the beginning of the storage, and the storage will not be truncated before starting to write.
9397

@@ -109,14 +113,12 @@ Other way is to upload a storage directly. Note that unlike with URLs, file uplo
109113
fully uploaded.
110114
```python
111115

112-
from pathlib import Path
113-
114116
new_storage = manager.create_storage(size=20, zone='de-fra1', title='New imported storage')
115117
storage_import = manager.create_storage_import(storage=new_storage.uuid, source='direct_upload')
116118

117119
manager.upload_file_for_storage_import(
118120
storage_import=storage_import,
119-
file=Path('/path/to/your/storage.img'),
121+
file='/path/to/your/storage.img',
120122
)
121123

122124
import_details = manager.get_storage_import_details(new_storage.uuid)

test/test_storage.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ def test_get_templates(self, manager):
3030
@responses.activate
3131
def test_storage_create(self, manager):
3232
Mock.mock_post("storage")
33-
storage = manager.create_storage(zone="fi-hel1", size=666, tier="maxiops", title="My data collection")
33+
storage = manager.create_storage(
34+
zone="fi-hel1", size=666, tier="maxiops", title="My data collection"
35+
)
3436
assert type(storage).__name__ == "Storage"
3537
assert storage.size == 666
3638
assert storage.tier == "maxiops"

upcloud_api/cloud_manager/storage_mixin.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
from os import PathLike
12
from typing import Optional, Union
2-
from pathlib import Path
33

44
from upcloud_api.api import API
55
from upcloud_api.storage import Storage
@@ -194,7 +194,13 @@ def create_storage_import(
194194
res = self.api.post_request(url, body)
195195
return StorageImport(**res['storage_import'])
196196

197-
def upload_file_for_storage_import(self, storage_import: StorageImport, file: Path, timeout: int = 600):
197+
def upload_file_for_storage_import(
198+
self,
199+
storage_import: StorageImport,
200+
file: 'PathLike[str]',
201+
timeout: int = 600,
202+
content_type: str = 'application/octet-stream',
203+
):
198204
"""
199205
Uploads a file directly to UpCloud's uploader session.
200206
"""
@@ -209,7 +215,7 @@ def upload_file_for_storage_import(self, storage_import: StorageImport, file: Pa
209215
resp = requests.put(
210216
url=storage_import.direct_upload_url,
211217
data=f,
212-
headers={'Content-type': 'application/octet-stream'},
218+
headers={'Content-type': content_type},
213219
timeout=timeout,
214220
)
215221

upcloud_api/utils.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,3 @@ def try_it_n_times(operation, expected_error_codes, custom_error='operation fail
2222
sleep(3)
2323
if i >= n - 1:
2424
raise UpCloudClientError(custom_error)
25-

0 commit comments

Comments
 (0)