Skip to content

Commit 0190de4

Browse files
GitHKAndrei Neagusanderegg
authored
Extending data_manager (#2196)
* added API call to query metadata entries * exposing to data_mater * added tests * fixing check * added more debugging * this should really be working * removing pointless statement * figuring out types * checking before uploading * using correct object * no need to assert this * assert it dose not exsit * Update packages/simcore-sdk/src/simcore_sdk/node_data/data_manager.py Co-authored-by: Sylvain <[email protected]> * applied renaming Co-authored-by: Andrei Neagu <[email protected]> Co-authored-by: Sylvain <[email protected]>
1 parent b895660 commit 0190de4

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

packages/simcore-sdk/src/simcore_sdk/node_data/data_manager.py

+10
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,13 @@ async def pull(file_or_folder: Path):
6969
archive_to_extract=str(archive_file), destination_folder=file_or_folder
7070
)
7171
log.info("extraction completed")
72+
73+
74+
async def is_file_present_in_storage(file_path: Path) -> bool:
75+
"""
76+
:retruns True if an entry is present inside the files_metadata else False
77+
"""
78+
return await filemanager.entry_exists(
79+
store_id=0, # this is for simcore.s3
80+
s3_object=_create_s3_object(file_path),
81+
)

packages/simcore-sdk/src/simcore_sdk/node_ports/filemanager.py

+18
Original file line numberDiff line numberDiff line change
@@ -297,3 +297,21 @@ async def upload_file(
297297
return store_id, e_tag
298298

299299
raise exceptions.S3InvalidPathError(s3_object)
300+
301+
302+
async def entry_exists(store_id: str, s3_object: str) -> bool:
303+
"""Returns True if metadata for s3_object is present"""
304+
user_id = config.USER_ID
305+
with api_client() as client:
306+
api = UsersApi(client)
307+
try:
308+
result = await api.get_file_metadata(s3_object, store_id, user_id)
309+
is_metadata_present = result.data.object_name == s3_object
310+
return is_metadata_present
311+
except Exception: # pylint: disable=broad-except
312+
log.warning(
313+
"There is no metadata for requested store_id=%s s3_object=%s",
314+
store_id,
315+
s3_object,
316+
)
317+
return False

packages/simcore-sdk/tests/integration/test_filemanager.py

+45
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,48 @@ async def test_invalid_store(
126126
await filemanager.download_file_from_s3(
127127
store_name=store, s3_object=file_id, local_folder=download_folder
128128
)
129+
130+
131+
async def test_valid_metadata(
132+
tmpdir: Path,
133+
bucket: str,
134+
filemanager_cfg: None,
135+
user_id: str,
136+
file_uuid: str,
137+
s3_simcore_location: str,
138+
):
139+
file_path = Path(tmpdir) / "test.test"
140+
file_path.write_text("I am a test file")
141+
assert file_path.exists()
142+
143+
file_id = file_uuid(file_path)
144+
store_id, e_tag = await filemanager.upload_file(
145+
store_id=s3_simcore_location, s3_object=file_id, local_file_path=file_path
146+
)
147+
assert store_id == s3_simcore_location
148+
assert e_tag
149+
150+
is_metadata_present = await filemanager.entry_exists(
151+
store_id=store_id, s3_object=file_id
152+
)
153+
154+
assert is_metadata_present is True
155+
156+
157+
async def test_invalid_metadata(
158+
tmpdir: Path,
159+
bucket: str,
160+
filemanager_cfg: None,
161+
user_id: str,
162+
file_uuid: str,
163+
s3_simcore_location: str,
164+
):
165+
file_path = Path(tmpdir) / "test.test"
166+
file_id = file_uuid(file_path)
167+
assert file_path.exists() is False
168+
169+
is_metadata_present = await filemanager.entry_exists(
170+
store_id=s3_simcore_location, s3_object=file_id
171+
)
172+
173+
assert is_metadata_present is False

0 commit comments

Comments
 (0)