msIntui commited on
Commit
58bdeac
·
1 Parent(s): 39d0031

Fix storage initialization with better fallback handling

Browse files
Files changed (1) hide show
  1. storage.py +24 -6
storage.py CHANGED
@@ -1,8 +1,12 @@
1
  import os
2
  import shutil
3
- from azure.storage.blob import BlobServiceClient
4
  from abc import ABC, abstractmethod
5
  import json
 
 
 
 
 
6
 
7
  class StorageInterface(ABC):
8
 
@@ -194,15 +198,29 @@ class BlobStorage(StorageInterface):
194
  class StorageFactory:
195
  @staticmethod
196
  def get_storage() -> StorageInterface:
197
- storage_type = os.getenv('STORAGE_TYPE', 'local').lower()
 
 
198
  if storage_type == 'local':
199
  return LocalStorage()
200
- elif storage_type == 'blob':
 
 
 
 
 
 
 
 
201
  connection_string = os.getenv('AZURE_STORAGE_CONNECTION_STRING')
202
  container_name = os.getenv('AZURE_STORAGE_CONTAINER_NAME')
 
 
203
  if not connection_string or not container_name:
204
- raise ValueError("Azure Blob Storage connection string and container name must be set")
 
 
205
  return BlobStorage(connection_string, container_name)
206
- else:
207
- raise ValueError(f"Unsupported storage type: {storage_type}")
208
 
 
1
  import os
2
  import shutil
 
3
  from abc import ABC, abstractmethod
4
  import json
5
+ try:
6
+ from azure.storage.blob import BlobServiceClient
7
+ AZURE_AVAILABLE = True
8
+ except ImportError:
9
+ AZURE_AVAILABLE = False
10
 
11
  class StorageInterface(ABC):
12
 
 
198
  class StorageFactory:
199
  @staticmethod
200
  def get_storage() -> StorageInterface:
201
+ storage_type = os.getenv('STORAGE_TYPE', 'local')
202
+
203
+ # Always return LocalStorage if STORAGE_TYPE is 'local'
204
  if storage_type == 'local':
205
  return LocalStorage()
206
+
207
+ # Handle Azure storage with fallback
208
+ if storage_type in ['azure', 'blob']:
209
+ # If Azure SDK isn't available, fall back to local
210
+ if not AZURE_AVAILABLE:
211
+ print("Warning: Azure Storage SDK not available, falling back to local storage")
212
+ return LocalStorage()
213
+
214
+ # Try to get Azure credentials
215
  connection_string = os.getenv('AZURE_STORAGE_CONNECTION_STRING')
216
  container_name = os.getenv('AZURE_STORAGE_CONTAINER_NAME')
217
+
218
+ # If credentials are missing, fall back to local
219
  if not connection_string or not container_name:
220
+ print("Warning: Azure credentials not found, falling back to local storage")
221
+ return LocalStorage()
222
+
223
  return BlobStorage(connection_string, container_name)
224
+
225
+ raise ValueError(f"Unsupported storage type: {storage_type}")
226