Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit 0100ff1

Browse files
feat: Implement info and exists file API methods (#318)
Co-authored-by: Andrew Smith <[email protected]>
1 parent 2699126 commit 0100ff1

File tree

4 files changed

+126
-0
lines changed

4 files changed

+126
-0
lines changed

storage3/_async/file_api.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,42 @@ async def remove(self, paths: list) -> list[dict[str, Any]]:
311311
)
312312
return response.json()
313313

314+
async def info(
315+
self,
316+
path: str,
317+
) -> list[dict[str, str]]:
318+
"""
319+
Lists info for a particular file.
320+
321+
Parameters
322+
----------
323+
path
324+
The path to the file.
325+
"""
326+
response = await self._request(
327+
"GET",
328+
f"/object/info/{self.id}/{path}",
329+
)
330+
return response.json()
331+
332+
async def exists(
333+
self,
334+
path: str,
335+
) -> bool:
336+
"""
337+
Returns True if the file exists, False otherwise.
338+
339+
Parameters
340+
----------
341+
path
342+
The path to the file.
343+
"""
344+
response = await self._request(
345+
"HEAD",
346+
f"/object/info/{self.id}/{path}",
347+
)
348+
return response.status_code == 200
349+
314350
async def list(
315351
self,
316352
path: Optional[str] = None,

storage3/_sync/file_api.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,42 @@ def remove(self, paths: list) -> list[dict[str, Any]]:
311311
)
312312
return response.json()
313313

314+
def info(
315+
self,
316+
path: str,
317+
) -> list[dict[str, str]]:
318+
"""
319+
Lists info for a particular file.
320+
321+
Parameters
322+
----------
323+
path
324+
The path to the file.
325+
"""
326+
response = self._request(
327+
"GET",
328+
f"/object/info/{self.id}/{path}",
329+
)
330+
return response.json()
331+
332+
def exists(
333+
self,
334+
path: str,
335+
) -> bool:
336+
"""
337+
Returns True if the file exists, False otherwise.
338+
339+
Parameters
340+
----------
341+
path
342+
The path to the file.
343+
"""
344+
response = self._request(
345+
"HEAD",
346+
f"/object/info/{self.id}/{path}",
347+
)
348+
return response.status_code == 200
349+
314350
def list(
315351
self,
316352
path: Optional[str] = None,

tests/_async/test_client.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,3 +396,30 @@ async def test_client_get_public_url(
396396
response.raise_for_status()
397397

398398
assert response.content == file.file_content
399+
400+
401+
async def test_client_info(
402+
storage_file_client_public: SyncBucketProxy, file: FileForTesting
403+
) -> None:
404+
"""Ensure we can get the public url of a file in a bucket"""
405+
await storage_file_client_public.upload(
406+
file.bucket_path, file.local_path, {"content-type": file.mime_type}
407+
)
408+
409+
info = await storage_file_client_public.info(file.bucket_path)
410+
assert "metadata" in info.keys()
411+
assert info["name"] == file.bucket_path
412+
assert info["content_type"] == file.mime_type
413+
414+
415+
async def test_client_exists(
416+
storage_file_client_public: SyncBucketProxy, file: FileForTesting
417+
) -> None:
418+
"""Ensure we can get the public url of a file in a bucket"""
419+
await storage_file_client_public.upload(
420+
file.bucket_path, file.local_path, {"content-type": file.mime_type}
421+
)
422+
423+
exists = await storage_file_client_public.exists(file.bucket_path)
424+
425+
assert exists

tests/_sync/test_client.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,3 +394,30 @@ def test_client_get_public_url(
394394
response.raise_for_status()
395395

396396
assert response.content == file.file_content
397+
398+
399+
def test_client_info(
400+
storage_file_client_public: SyncBucketProxy, file: FileForTesting
401+
) -> None:
402+
"""Ensure we can get the public url of a file in a bucket"""
403+
storage_file_client_public.upload(
404+
file.bucket_path, file.local_path, {"content-type": file.mime_type}
405+
)
406+
407+
info = storage_file_client_public.info(file.bucket_path)
408+
assert "metadata" in info.keys()
409+
assert info["name"] == file.bucket_path
410+
assert info["content_type"] == file.mime_type
411+
412+
413+
def test_client_exists(
414+
storage_file_client_public: SyncBucketProxy, file: FileForTesting
415+
) -> None:
416+
"""Ensure we can get the public url of a file in a bucket"""
417+
storage_file_client_public.upload(
418+
file.bucket_path, file.local_path, {"content-type": file.mime_type}
419+
)
420+
421+
exists = storage_file_client_public.exists(file.bucket_path)
422+
423+
assert exists

0 commit comments

Comments
 (0)