Skip to content

Commit

Permalink
Added functionality to list all bucket object IDs (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
mephenor authored May 25, 2023
1 parent 32f3ae5 commit 4a4ed22
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion hexkit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@

"""A Toolkit for Building Microservices using the Hexagonal Architecture"""

__version__ = "0.9.3"
__version__ = "0.10.0"
17 changes: 17 additions & 0 deletions hexkit/protocols/objstorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ async def delete_bucket(
self._validate_bucket_id(bucket_id)
await self._delete_bucket(bucket_id, delete_content=delete_content)

async def list_all_object_ids(self, bucket_id: str) -> list[str]:
"""
Retrieve a list of IDs for all objects currently present in the specified bucket
"""
self._validate_bucket_id(bucket_id)
return await self._list_all_object_ids(bucket_id=bucket_id)

async def get_object_upload_url(
self,
*,
Expand Down Expand Up @@ -293,6 +300,16 @@ async def _delete_bucket(
"""
...

@abstractmethod
async def _list_all_object_ids(self, *, bucket_id: str) -> list[str]:
"""
Retrieve a list of IDs for all objects currently present in the specified bucket
*To be implemented by the provider. Input validation is done outside of this
method.*
"""
...

@abstractmethod
async def _get_object_upload_url(
self,
Expand Down
15 changes: 15 additions & 0 deletions hexkit/providers/s3/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,21 @@ async def _delete_bucket(
error, bucket_id=bucket_id
) from error

async def _list_all_object_ids(self, *, bucket_id: str) -> list[str]:
"""
Retrieve a list of IDs for all objects currently present in the specified bucket
"""
await self._assert_bucket_exists(bucket_id)

try:
bucket = self._resource.Bucket(bucket_id)
content = await asyncio.to_thread(bucket.objects.all)
return [object_summary.key for object_summary in content]
except botocore.exceptions.ClientError as error:
raise self._translate_s3_client_errors(
error, bucket_id=bucket_id
) from error

async def _does_object_exist(
self, *, bucket_id: str, object_id: str, object_md5sum: Optional[str] = None
) -> bool:
Expand Down
20 changes: 20 additions & 0 deletions tests/integration/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,26 @@ async def test_get_object_size(
assert expected_size == observed_size


@pytest.mark.asyncio
async def test_list_all_object_ids(
s3_fixture: S3Fixture, # noqa: F811
file_fixture: FileObject, # noqa: F811
):
"""Test if listing all object IDs for a bucket works correctly."""

file_fixture2 = file_fixture.copy(deep=True)
file_fixture2.object_id = "mydefaulttestobject002"

# add file objects to storage
await s3_fixture.populate_file_objects([file_fixture, file_fixture2])

# retrieve all object ids
retrieved_ids = await s3_fixture.storage.list_all_object_ids(
bucket_id=file_fixture.bucket_id
)
assert retrieved_ids == [file_fixture.object_id, file_fixture2.object_id]


@pytest.mark.asyncio
async def test_bucket_existence_checks(s3_fixture: S3Fixture): # noqa: F811
"""Test if the checks for existence of buckets work correctly."""
Expand Down

0 comments on commit 4a4ed22

Please sign in to comment.