Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/apify/storage_clients/_apify/_alias_resolving.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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_unnamed__'

def __init__(
self,
storage_type: Literal['Dataset', 'KeyValueStore', 'RequestQueue'],
Expand Down Expand Up @@ -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'],
Expand Down
4 changes: 2 additions & 2 deletions src/apify/storage_clients/_apify/_api_client_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions tests/integration/test_storages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Loading