diff --git a/src/apify/storage_clients/_apify/_alias_resolving.py b/src/apify/storage_clients/_apify/_alias_resolving.py index 1170193b..c07edca3 100644 --- a/src/apify/storage_clients/_apify/_alias_resolving.py +++ b/src/apify/storage_clients/_apify/_alias_resolving.py @@ -76,7 +76,7 @@ async def open_by_alias( The alias mapping is stored in the default key-value store for persistence across Actor runs. Args: - alias: The alias name for the storage (e.g., '__default__', 'my-storage'). + alias: The alias name for the storage (e.g., 'my-storage'). storage_type: The type of storage to open. collection_client: The Apify API collection client for the storage type. get_resource_client_by_id: A callable that takes a storage ID and returns the resource client. @@ -131,6 +131,8 @@ class AliasResolver: _alias_init_lock: Lock | None = None """Lock for creating alias storages. Only one alias storage can be created at the time. Global for all instances.""" + default_storage_key: ClassVar[str] = '__default__' + def __init__( self, storage_type: Literal['Dataset', 'KeyValueStore', 'RequestQueue'], @@ -195,7 +197,7 @@ async def resolve_id(self) -> str | None: """ # First try to find the alias in the configuration mapping to avoid any API calls. # This mapping is maintained by the Apify platform and does not have to be maintained in the default KVS. - if self._configuration.actor_storages and self._alias != 'default': + if self._configuration.actor_storages: storage_maps = { 'Dataset': self._configuration.actor_storages['datasets'], 'KeyValueStore': self._configuration.actor_storages['key_value_stores'], diff --git a/src/apify/storage_clients/_apify/_api_client_creation.py b/src/apify/storage_clients/_apify/_api_client_creation.py index 39e2a087..542a203f 100644 --- a/src/apify/storage_clients/_apify/_api_client_creation.py +++ b/src/apify/storage_clients/_apify/_api_client_creation.py @@ -5,7 +5,7 @@ from apify_client import ApifyClientAsync from crawlee._utils.crypto import crypto_random_object_id -from apify.storage_clients._apify._alias_resolving import open_by_alias +from apify.storage_clients._apify._alias_resolving import AliasResolver, open_by_alias if TYPE_CHECKING: from apify_client.clients import DatasetClientAsync, KeyValueStoreClientAsync, RequestQueueClientAsync @@ -113,7 +113,7 @@ def get_resource_client(storage_id: str) -> DatasetClientAsync: # Normalize unnamed default storage to unnamed storage aliased as `__default__`. case (None, None, None, None): return await open_by_alias( - alias='__default__', + alias=AliasResolver.default_storage_key, storage_type=storage_type, collection_client=collection_client, get_resource_client_by_id=get_resource_client, diff --git a/tests/e2e/test_schema_storages/actor_source/main.py b/tests/e2e/test_schema_storages/actor_source/main.py index 43a82207..454d3bf7 100644 --- a/tests/e2e/test_schema_storages/actor_source/main.py +++ b/tests/e2e/test_schema_storages/actor_source/main.py @@ -7,3 +7,7 @@ async def main() -> None: dataset = await Actor.open_dataset(alias='custom') expected_id = Actor.configuration.actor_storages['datasets']['custom'] assert dataset.id == expected_id + + implicit_default_dataset = await Actor.open_dataset() + explicit_default_dataset = await Actor.open_dataset(alias='default') + assert implicit_default_dataset.id == explicit_default_dataset.id diff --git a/tests/integration/test_storages.py b/tests/integration/test_storages.py index 7ad807fe..d09036d8 100644 --- a/tests/integration/test_storages.py +++ b/tests/integration/test_storages.py @@ -9,6 +9,7 @@ from apify import Actor, Configuration from apify.storage_clients import ApifyStorageClient, MemoryStorageClient, SmartApifyStorageClient +from apify.storage_clients._apify._alias_resolving import AliasResolver @pytest.mark.parametrize( @@ -125,3 +126,11 @@ async def test_actor_implicit_storage_init(apify_token: str) -> None: assert await Actor.open_dataset() is not await Actor.open_dataset(force_cloud=True) assert await Actor.open_key_value_store() is not await Actor.open_key_value_store(force_cloud=True) assert await Actor.open_request_queue() is not await Actor.open_request_queue(force_cloud=True) + + +async def test_default_storage(apify_token: str) -> None: + service_locator.set_configuration(Configuration(token=apify_token)) + async with Actor: + dataset_1 = await Actor.open_dataset(force_cloud=True) + dataset_2 = await Actor.open_dataset(force_cloud=True, alias=AliasResolver.default_storage_key) + assert dataset_1.id == dataset_2.id