Skip to content

Commit 14dc446

Browse files
author
Andrei Neagu
committed
only keep selection
1 parent 0fb8613 commit 14dc446

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

services/storage/src/simcore_service_storage/simcore_s3_dsm.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
from .modules.s3 import get_s3_client
8282
from .utils.s3_utils import S3TransferDataCB
8383
from .utils.simcore_s3_dsm_utils import (
84+
UserSelection,
8485
compute_file_id_prefix,
8586
create_and_upload_export,
8687
create_random_export_name,
@@ -1249,7 +1250,7 @@ async def create_s3_export(
12491250
*,
12501251
progress_bar: ProgressBarData,
12511252
) -> StorageFileID:
1252-
source_object_keys: set[StorageFileID] = set()
1253+
source_object_keys: set[tuple[UserSelection, StorageFileID]] = set()
12531254

12541255
# check access rights
12551256
for object_key in object_keys:
@@ -1279,7 +1280,7 @@ async def create_s3_export(
12791280
self.simcore_bucket_name, object_key
12801281
):
12811282
for entry in meta_data_files:
1282-
source_object_keys.add(entry.object_key)
1283+
source_object_keys.add((object_key, entry.object_key))
12831284

12841285
_logger.debug(
12851286
"User selection '%s' includes '%s' files",

services/storage/src/simcore_service_storage/utils/simcore_s3_dsm_utils.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from contextlib import suppress
22
from pathlib import Path
3+
from typing import TypeAlias
34
from uuid import uuid4
45

56
import orjson
67
from aws_library.s3 import S3MetaData, SimcoreS3API
78
from aws_library.s3._constants import STREAM_READER_CHUNK_SIZE
9+
from aws_library.s3._models import S3ObjectKey
810
from models_library.api_schemas_storage.storage_schemas import S3BucketName
911
from models_library.projects import ProjectID
1012
from models_library.projects_nodes_io import (
@@ -143,20 +145,33 @@ def create_random_export_name(user_id: UserID) -> StorageFileID:
143145
)
144146

145147

148+
UserSelection: TypeAlias = str
149+
150+
151+
def _strip_parent(selection: UserSelection, s3_object: S3ObjectKey) -> str:
152+
selection_path = Path(selection)
153+
s3_object_path = Path(s3_object)
154+
if selection_path == s3_object_path:
155+
return s3_object_path.name
156+
157+
result = s3_object_path.relative_to(selection_path)
158+
return f"{result}"
159+
160+
146161
async def create_and_upload_export(
147162
s3_client: SimcoreS3API,
148163
bucket: S3BucketName,
149164
*,
150-
source_object_keys: set[StorageFileID],
165+
source_object_keys: set[tuple[UserSelection, StorageFileID]],
151166
destination_object_keys: StorageFileID,
152167
progress_bar: ProgressBarData,
153168
) -> None:
154169
archive_entries: ArchiveEntries = [
155170
(
156-
s3_object,
171+
_strip_parent(selection, s3_object),
157172
await s3_client.get_bytes_streamer_from_object(bucket, s3_object),
158173
)
159-
for s3_object in source_object_keys
174+
for (selection, s3_object) in source_object_keys
160175
]
161176

162177
async with progress_bar:

services/storage/tests/unit/test_simcore_s3_dsm_utils.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
from pathlib import Path
2+
13
import pytest
2-
from simcore_service_storage.utils.simcore_s3_dsm_utils import compute_file_id_prefix
4+
from aws_library.s3._models import S3ObjectKey
5+
from simcore_service_storage.utils.simcore_s3_dsm_utils import (
6+
UserSelection,
7+
_strip_parent,
8+
compute_file_id_prefix,
9+
)
310

411

512
@pytest.mark.parametrize(
@@ -19,3 +26,23 @@
1926
)
2027
def test_compute_file_id_prefix(file_id, levels, expected):
2128
assert compute_file_id_prefix(file_id, levels) == expected
29+
30+
31+
_FOLDERS_PATH = Path("nested/folders/path")
32+
33+
34+
@pytest.mark.parametrize(
35+
"selection, s3_object, expected",
36+
[
37+
(Path("single_file"), Path("single_file"), "single_file"),
38+
(Path("single_folder"), Path("single_folder"), "single_folder"),
39+
(_FOLDERS_PATH / "folder", _FOLDERS_PATH / "folder", "folder"),
40+
(_FOLDERS_PATH / "a_file.txt", _FOLDERS_PATH / "a_file.txt", "a_file.txt"),
41+
(_FOLDERS_PATH, _FOLDERS_PATH / "the/actual/path", "the/actual/path"),
42+
],
43+
)
44+
def test__strip_parent(selection: Path, s3_object: Path, expected: str):
45+
assert (
46+
_strip_parent(UserSelection(f"{selection}"), S3ObjectKey(f"{s3_object}"))
47+
== expected
48+
)

0 commit comments

Comments
 (0)