File tree Expand file tree Collapse file tree 11 files changed +175
-114
lines changed
src/unstract/sdk/file_storage Expand file tree Collapse file tree 11 files changed +175
-114
lines changed Original file line number Diff line number Diff line change 66 "SharedTemporaryFileStorage" ,
77]
88
9- from unstract .sdk .file_storage .fs_impl import FileStorage
10- from unstract .sdk .file_storage .fs_permanent import PermanentFileStorage
11- from unstract .sdk .file_storage .fs_provider import FileStorageProvider
12- from unstract .sdk .file_storage .fs_shared_temporary import SharedTemporaryFileStorage
139from unstract .sdk .file_storage .helper import FileStorageHelper
10+ from unstract .sdk .file_storage .impl import FileStorage
11+ from unstract .sdk .file_storage .permanent import PermanentFileStorage
12+ from unstract .sdk .file_storage .provider import FileStorageProvider
13+ from unstract .sdk .file_storage .shared_temporary import SharedTemporaryFileStorage
Original file line number Diff line number Diff line change 1+ from enum import Enum
2+
3+
14class FileOperationParams :
25 READ_ENTIRE_LENGTH = - 1
36 DEFAULT_ENCODING = "utf-8"
@@ -7,3 +10,13 @@ class FileSeekPosition:
710 START = 0
811 CURRENT = 1
912 END = 2
13+
14+
15+ class StorageType (Enum ):
16+ PERMANENT = "permanent"
17+ TEMPORARY = "temporary"
18+
19+
20+ class CredentialKeyword :
21+ PROVIDER = "provider"
22+ CREDENTIALS = "credentials"
Original file line number Diff line number Diff line change 1+ import json
2+ import logging
3+ import os
4+
5+ from unstract .sdk .exceptions import FileStorageError
6+ from unstract .sdk .file_storage .constants import CredentialKeyword , StorageType
7+ from unstract .sdk .file_storage .impl import FileStorage
8+ from unstract .sdk .file_storage .permanent import PermanentFileStorage
9+ from unstract .sdk .file_storage .provider import FileStorageProvider
10+ from unstract .sdk .file_storage .shared_temporary import SharedTemporaryFileStorage
11+
12+ logger = logging .getLogger (__name__ )
13+
14+
15+ class EnvHelper :
16+ @staticmethod
17+ def get_storage (storage_type : StorageType , env_name : str ) -> FileStorage :
18+ try :
19+ file_storage_creds = json .loads (os .environ .get (env_name ))
20+ provider = FileStorageProvider (
21+ file_storage_creds [CredentialKeyword .PROVIDER ]
22+ )
23+ credentials = file_storage_creds .get (CredentialKeyword .CREDENTIALS , {})
24+ if storage_type == StorageType .PERMANENT .value :
25+ file_storage = PermanentFileStorage (provider = provider , ** credentials )
26+ elif storage_type == StorageType .TEMPORARY .value :
27+ file_storage = SharedTemporaryFileStorage (
28+ provider = provider , ** credentials
29+ )
30+ else :
31+ raise NotImplementedError ()
32+ return file_storage
33+ except KeyError as e :
34+ logger .error (f"Required credentials is missing in the env: { str (e )} " )
35+ raise e
36+ except FileStorageError as e :
37+ raise e
Original file line number Diff line number Diff line change 44import fsspec
55from fsspec import AbstractFileSystem
66
7- from unstract .sdk .exceptions import FileStorageError
8- from unstract .sdk .file_storage .fs_provider import FileStorageProvider
7+ from unstract .sdk .exceptions import FileOperationError , FileStorageError
8+ from unstract .sdk .file_storage .provider import FileStorageProvider
99
1010logger = logging .getLogger (__name__ )
1111
@@ -67,3 +67,24 @@ def local_file_system_init() -> AbstractFileSystem:
6767 f" file system { e } "
6868 )
6969 raise FileStorageError (str (e )) from e
70+
71+
72+ def skip_local_cache (func ):
73+ def wrapper (* args , ** kwargs ):
74+ try :
75+ return func (* args , ** kwargs )
76+ except FileNotFoundError :
77+ try :
78+ # FileNotFound could have been caused by stale cache.
79+ # Hence invalidate cache and retry again
80+ args [0 ].fs .invalidate_cache ()
81+ return func (* args , ** kwargs )
82+ except Exception as e :
83+ if isinstance (e , FileNotFoundError ):
84+ raise e
85+ else :
86+ raise FileOperationError (str (e )) from e
87+ except Exception as e :
88+ raise FileOperationError (str (e )) from e
89+
90+ return wrapper
You can’t perform that action at this time.
0 commit comments