Skip to content

Commit 1008fa5

Browse files
committed
fix alias resolution
1 parent d2c79d3 commit 1008fa5

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

src/apify/storage_clients/_apify/_utils.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
logger = getLogger(__name__)
1414

15+
_ALIAS_MAPPING_KEY = '__STORAGE_ALIASES_MAPPING'
16+
1517

1618
async def resolve_alias_to_id(
1719
alias: str,
@@ -30,22 +32,20 @@ async def resolve_alias_to_id(
3032
"""
3133
default_kvs_client = await _get_default_kvs_client(configuration)
3234

33-
# Create the key for this alias
35+
# Create the dictionary key for this alias.
3436
alias_key = f'alias-{storage_type}-{alias}'
3537

36-
# Try to get the stored ID for this alias
3738
try:
38-
record = await default_kvs_client.get_record(alias_key)
39-
if record and record.get('value'):
40-
# The record structure is: {'value': {'value': 'storage_id'}}
41-
value_data = record['value']
42-
if isinstance(value_data, dict) and 'value' in value_data:
43-
storage_id = value_data['value']
44-
if isinstance(storage_id, str):
45-
return storage_id
46-
except Exception as e:
47-
# If there's any error accessing the record, treat it as not found
48-
logger.warning(f'Error accessing alias mapping for {alias}: {e}')
39+
record = await default_kvs_client.get_record(_ALIAS_MAPPING_KEY)
40+
41+
# Extract the actual data from the KVS record
42+
if isinstance(record, dict) and alias_key in record:
43+
storage_id = record[alias_key]
44+
return str(storage_id)
45+
46+
except Exception as exc:
47+
# If there's any error accessing the record, treat it as not found.
48+
logger.warning(f'Error accessing alias mapping for {alias}: {exc}')
4949

5050
return None
5151

@@ -66,11 +66,22 @@ async def store_alias_mapping(
6666
"""
6767
default_kvs_client = await _get_default_kvs_client(configuration)
6868

69-
# Create the key for this alias (must match the format in resolve_alias_to_id)
69+
# Create the dictionary key for this alias.
7070
alias_key = f'alias-{storage_type}-{alias}'
7171

72-
# Store the mapping
73-
await default_kvs_client.set_record(alias_key, {'value': storage_id})
72+
try:
73+
record = await default_kvs_client.get_record(_ALIAS_MAPPING_KEY)
74+
75+
# Update or create the record with the new alias mapping
76+
if isinstance(record, dict):
77+
record[alias_key] = storage_id
78+
else:
79+
record = {alias_key: storage_id}
80+
81+
# Store the mapping back in the KVS.
82+
await default_kvs_client.set_record(_ALIAS_MAPPING_KEY, record)
83+
except Exception as exc:
84+
logger.warning(f'Error accessing alias mapping for {alias}: {exc}')
7485

7586

7687
async def _get_default_kvs_client(configuration: Configuration) -> KeyValueStoreClientAsync:

0 commit comments

Comments
 (0)