88import gzip
99import logging
1010import uuid
11- from typing import TYPE_CHECKING , Optional
11+ from typing import Optional
1212
1313from azure .storage .blob import BlobServiceClient
14-
15- if TYPE_CHECKING :
16- from azure .storage .blob .aio import BlobServiceClient as AsyncBlobServiceClient
14+ from azure .storage .blob .aio import BlobServiceClient as AsyncBlobServiceClient
1715
1816from durabletask .extensions .azure_blob_payloads .options import BlobPayloadStoreOptions
1917from durabletask .payload .store import PayloadStore
@@ -69,35 +67,25 @@ def __init__(self, options: BlobPayloadStoreOptions):
6967 ** extra_kwargs ,
7068 )
7169
72- # Async client is built lazily to avoid importing
73- # azure.storage.blob.aio when only sync methods are used.
74- self ._async_blob_service_client : AsyncBlobServiceClient | None = None
75- self ._extra_kwargs = extra_kwargs
70+ # Build async client
71+ if options .connection_string :
72+ self ._async_blob_service_client = AsyncBlobServiceClient .from_connection_string (
73+ options .connection_string , ** extra_kwargs ,
74+ )
75+ else :
76+ assert options .account_url is not None # guaranteed by validation above
77+ self ._async_blob_service_client = AsyncBlobServiceClient (
78+ account_url = options .account_url ,
79+ credential = options .credential ,
80+ ** extra_kwargs ,
81+ )
7682
7783 self ._ensure_container_created = False
7884
7985 @property
8086 def options (self ) -> BlobPayloadStoreOptions :
8187 return self ._options
8288
83- def _get_async_blob_service_client (self ) -> AsyncBlobServiceClient :
84- """Lazily create the async blob service client."""
85- if self ._async_blob_service_client is None :
86- from azure .storage .blob .aio import BlobServiceClient as AsyncBlobServiceClient
87-
88- if self ._options .connection_string :
89- self ._async_blob_service_client = AsyncBlobServiceClient .from_connection_string (
90- self ._options .connection_string , ** self ._extra_kwargs ,
91- )
92- else :
93- assert self ._options .account_url is not None
94- self ._async_blob_service_client = AsyncBlobServiceClient (
95- account_url = self ._options .account_url ,
96- credential = self ._options .credential ,
97- ** self ._extra_kwargs ,
98- )
99- return self ._async_blob_service_client
100-
10189 # ------------------------------------------------------------------
10290 # Sync operations
10391 # ------------------------------------------------------------------
@@ -138,8 +126,7 @@ async def upload_async(self, data: bytes, *, instance_id: Optional[str] = None)
138126 data = gzip .compress (data )
139127
140128 blob_name = self ._make_blob_name (instance_id )
141- client = self ._get_async_blob_service_client ()
142- container_client = client .get_container_client (self ._container_name )
129+ container_client = self ._async_blob_service_client .get_container_client (self ._container_name )
143130 await container_client .upload_blob (name = blob_name , data = data , overwrite = True )
144131
145132 token = f"{ _TOKEN_PREFIX } { self ._container_name } :{ blob_name } "
@@ -148,8 +135,7 @@ async def upload_async(self, data: bytes, *, instance_id: Optional[str] = None)
148135
149136 async def download_async (self , token : str ) -> bytes :
150137 container , blob_name = self ._parse_token (token )
151- client = self ._get_async_blob_service_client ()
152- container_client = client .get_container_client (container )
138+ container_client = self ._async_blob_service_client .get_container_client (container )
153139 stream = await container_client .download_blob (blob_name )
154140 blob_data = await stream .readall ()
155141
@@ -164,7 +150,11 @@ async def download_async(self, token: str) -> bytes:
164150 # ------------------------------------------------------------------
165151
166152 def is_known_token (self , value : str ) -> bool :
167- return value .startswith (_TOKEN_PREFIX )
153+ try :
154+ self ._parse_token (value )
155+ return True
156+ except ValueError :
157+ return False
168158
169159 @staticmethod
170160 def _parse_token (token : str ) -> tuple [str , str ]:
@@ -203,8 +193,7 @@ def _ensure_container_sync(self) -> None:
203193 async def _ensure_container_async (self ) -> None :
204194 if self ._ensure_container_created :
205195 return
206- client = self ._get_async_blob_service_client ()
207- container_client = client .get_container_client (self ._container_name )
196+ container_client = self ._async_blob_service_client .get_container_client (self ._container_name )
208197 try :
209198 await container_client .create_container ()
210199 except Exception :
0 commit comments