microsoft-agents-storage-blob 0.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of microsoft-agents-storage-blob might be problematic. Click here for more details.
- microsoft/agents/storage/blob/__init__.py +4 -0
- microsoft/agents/storage/blob/blob_storage.py +100 -0
- microsoft/agents/storage/blob/blob_storage_config.py +28 -0
- microsoft_agents_storage_blob-0.0.0.dist-info/METADATA +14 -0
- microsoft_agents_storage_blob-0.0.0.dist-info/RECORD +7 -0
- microsoft_agents_storage_blob-0.0.0.dist-info/WHEEL +5 -0
- microsoft_agents_storage_blob-0.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from typing import TypeVar, Union
|
|
3
|
+
from io import BytesIO
|
|
4
|
+
|
|
5
|
+
from azure.storage.blob.aio import (
|
|
6
|
+
ContainerClient,
|
|
7
|
+
BlobServiceClient,
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
from microsoft.agents.hosting.core.storage import StoreItem
|
|
11
|
+
from microsoft.agents.hosting.core.storage.storage import AsyncStorageBase
|
|
12
|
+
from microsoft.agents.hosting.core.storage._type_aliases import JSON
|
|
13
|
+
from microsoft.agents.hosting.core.storage.error_handling import (
|
|
14
|
+
ignore_error,
|
|
15
|
+
is_status_code_error,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
from .blob_storage_config import BlobStorageConfig
|
|
19
|
+
|
|
20
|
+
StoreItemT = TypeVar("StoreItemT", bound=StoreItem)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class BlobStorage(AsyncStorageBase):
|
|
24
|
+
|
|
25
|
+
def __init__(self, config: BlobStorageConfig):
|
|
26
|
+
|
|
27
|
+
if not config.container_name:
|
|
28
|
+
raise ValueError("BlobStorage: Container name is required.")
|
|
29
|
+
|
|
30
|
+
self.config = config
|
|
31
|
+
|
|
32
|
+
blob_service_client: BlobServiceClient = self._create_client()
|
|
33
|
+
self._container_client: ContainerClient = (
|
|
34
|
+
blob_service_client.get_container_client(config.container_name)
|
|
35
|
+
)
|
|
36
|
+
self._initialized: bool = False
|
|
37
|
+
|
|
38
|
+
def _create_client(self) -> BlobServiceClient:
|
|
39
|
+
if self.config.url: # connect with URL and credentials
|
|
40
|
+
if not self.config.credential:
|
|
41
|
+
raise ValueError(
|
|
42
|
+
"BlobStorage: Credential is required when using a custom service URL."
|
|
43
|
+
)
|
|
44
|
+
return BlobServiceClient(
|
|
45
|
+
account_url=self.config.url, credential=self.config.credential
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
else: # connect with connection string
|
|
49
|
+
return BlobServiceClient.from_connection_string(
|
|
50
|
+
self.config.connection_string
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
async def initialize(self) -> None:
|
|
54
|
+
"""Initializes the storage container"""
|
|
55
|
+
if not self._initialized:
|
|
56
|
+
# This should only happen once - assuming this is a singleton.
|
|
57
|
+
await ignore_error(
|
|
58
|
+
self._container_client.create_container(), is_status_code_error(409)
|
|
59
|
+
)
|
|
60
|
+
self._initialized = True
|
|
61
|
+
|
|
62
|
+
async def _read_item(
|
|
63
|
+
self, key: str, *, target_cls: StoreItemT = None, **kwargs
|
|
64
|
+
) -> tuple[Union[str, None], Union[StoreItemT, None]]:
|
|
65
|
+
item = await ignore_error(
|
|
66
|
+
self._container_client.download_blob(blob=key),
|
|
67
|
+
is_status_code_error(404),
|
|
68
|
+
)
|
|
69
|
+
if not item:
|
|
70
|
+
return None, None
|
|
71
|
+
|
|
72
|
+
item_rep: str = await item.readall()
|
|
73
|
+
item_JSON: JSON = json.loads(item_rep)
|
|
74
|
+
try:
|
|
75
|
+
return key, target_cls.from_json_to_store_item(item_JSON)
|
|
76
|
+
except AttributeError as error:
|
|
77
|
+
raise TypeError(
|
|
78
|
+
f"BlobStorage.read_item(): could not deserialize blob item into {target_cls} class. Error: {error}"
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
async def _write_item(self, key: str, item: StoreItem) -> None:
|
|
82
|
+
item_JSON: JSON = item.store_item_to_json()
|
|
83
|
+
if item_JSON is None:
|
|
84
|
+
raise ValueError(
|
|
85
|
+
"BlobStorage.write(): StoreItem serialization cannot return None"
|
|
86
|
+
)
|
|
87
|
+
item_rep_bytes = json.dumps(item_JSON).encode("utf-8")
|
|
88
|
+
|
|
89
|
+
# getting the length is important for performance with large blobs
|
|
90
|
+
await self._container_client.upload_blob(
|
|
91
|
+
name=key,
|
|
92
|
+
data=BytesIO(item_rep_bytes),
|
|
93
|
+
overwrite=True,
|
|
94
|
+
length=len(item_rep_bytes),
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
async def _delete_item(self, key: str) -> None:
|
|
98
|
+
await ignore_error(
|
|
99
|
+
self._container_client.delete_blob(blob=key), is_status_code_error(404)
|
|
100
|
+
)
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
from typing import Union
|
|
2
|
+
|
|
3
|
+
from azure.core.credentials import TokenCredential
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class BlobStorageConfig:
|
|
7
|
+
"""Configuration settings for BlobStorage."""
|
|
8
|
+
|
|
9
|
+
def __init__(
|
|
10
|
+
self,
|
|
11
|
+
container_name: str,
|
|
12
|
+
connection_string: str = "",
|
|
13
|
+
url: str = "",
|
|
14
|
+
credential: Union[TokenCredential, None] = None,
|
|
15
|
+
):
|
|
16
|
+
"""Configuration settings for BlobStorage.
|
|
17
|
+
|
|
18
|
+
container_name: The name of the blob container.
|
|
19
|
+
connection_string: The connection string to the storage account.
|
|
20
|
+
url: The URL of the blob service. If provided, credential must also be provided.
|
|
21
|
+
credential: The TokenCredential to use for authentication when using a custom URL.
|
|
22
|
+
|
|
23
|
+
credential-based authentication is prioritized over connection string authentication.
|
|
24
|
+
"""
|
|
25
|
+
self.container_name: str = container_name
|
|
26
|
+
self.connection_string: str = connection_string
|
|
27
|
+
self.url: str = url
|
|
28
|
+
self.credential: Union[TokenCredential, None] = credential
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: microsoft-agents-storage-blob
|
|
3
|
+
Version: 0.0.0
|
|
4
|
+
Summary: A blob storage library for Microsoft Agents
|
|
5
|
+
Author: Microsoft Corporation
|
|
6
|
+
Project-URL: Homepage, https://github.com/microsoft/Agents
|
|
7
|
+
Classifier: Programming Language :: Python :: 3
|
|
8
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
9
|
+
Classifier: Operating System :: OS Independent
|
|
10
|
+
Requires-Python: >=3.9
|
|
11
|
+
Requires-Dist: microsoft-agents-hosting-core==0.0.0
|
|
12
|
+
Requires-Dist: azure-core
|
|
13
|
+
Requires-Dist: azure-storage-blob
|
|
14
|
+
Dynamic: requires-dist
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
microsoft/agents/storage/blob/__init__.py,sha256=5IxnH_hkyJrp5Gdk92cfK78d39m_DAEyjX-iFr9g1Do,137
|
|
2
|
+
microsoft/agents/storage/blob/blob_storage.py,sha256=xbOAJzVhT4YRPKBtFDWpmPkPT3LWHqEFPlXdZ8I7teM,3534
|
|
3
|
+
microsoft/agents/storage/blob/blob_storage_config.py,sha256=wKK7WJAdy4FIIx5HCVIDNecPefZB5Hy6jM82gKdCBF8,1009
|
|
4
|
+
microsoft_agents_storage_blob-0.0.0.dist-info/METADATA,sha256=wCv1rah5-pQZ5S7_n2b5O2-gWUlBp80Ss56ovodVnKE,519
|
|
5
|
+
microsoft_agents_storage_blob-0.0.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
6
|
+
microsoft_agents_storage_blob-0.0.0.dist-info/top_level.txt,sha256=egwWDmpnNBTGerc55Oa6VVW9hTKdtxFjHWHywqmVMpM,10
|
|
7
|
+
microsoft_agents_storage_blob-0.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
microsoft
|