>Azure Blob 存储 是 Microsoft 的云对象存储解决方案。Blob 存储针对存储海量非结构化数据进行了优化。非结构化数据是不遵循特定数据模型或定义的数据,例如文本或二进制数据。
Azure Blob Storage 设计用于:
- - 直接向浏览器提供图像或文档。
- - 存储文件以供分布式访问。
- - 流式传输视频和音频。
- - 写入日志文件。
- - 存储数据以用于备份和还原、灾难恢复以及归档。
- - 存储数据以供本地或 Azure 托管服务进行分析。
本笔记本介绍如何从 Azure Blob Storage上的容器加载文档对象 Azure Blob 存储加载器 API 参考.
从容器加载
pip install -qU langchain-azure-storage
from langchain_azure_storage.document_loaders import AzureBlobStorageLoader
从容器
加载所有 blob,需要提供 AzureBlobStorageLoader account URL 和 container name 账户 URL 和容器名称。加载器返回 Document 对象,包含 blob 内容(默认为 UTF-8 编码)和元数据(包括 blob URL),如下例所示。
无需显式配置凭据,因为它使用 DefaultAzureCredential,后者会根据当前环境自动检索 Microsoft Entra ID 令牌 。
loader = AzureBlobStorageLoader(
"https://<storage-account-name>.blob.core.windows.net",
"<container-name>",
)
for doc in loader.load():
print(doc)
page_content='Lorem ipsum dolor sit amet.' metadata={'source': 'https://<storage-account-name>.blob.core.windows.net/<container-name>/<blob-name>'}
你也可以指定前缀,只返回以该前缀开头的 blob。
loader = AzureBlobStorageLoader(
"https://<storage-account-name>.blob.core.windows.net",
"<container-name>",
prefix="<prefix>",
)
按 blob 名称从容器加载
你可以从 blob 名称列表加载文档,这只会使用提供的 blob,而不会调用列出 blob 的 API。
loader = AzureBlobStorageLoader(
"https://<storage-account-name>.blob.core.windows.net",
"<container-name>",
blob_names=["blob-1", "blob-2", "blob-3"],
)
覆盖默认凭据
默认情况下,文档加载器使用 DefaultAzureCredential。以下示例展示如何覆盖此默认设置:
from azure.core.credentials import AzureSasCredential
from azure.identity import ManagedIdentityCredential
from langchain_azure_storage.document_loaders import AzureBlobStorageLoader
# Override with SAS token
loader = AzureBlobStorageLoader(
"https://<storage-account-name>.blob.core.windows.net",
"<container-name>",
credential=AzureSasCredential("<sas-token>")
)
# Override with more specific token credential than the entire
# default credential chain (e.g., system-assigned managed identity)
loader = AzureBlobStorageLoader(
"https://<storage-account-name>.blob.core.windows.net",
"<container-name>",
credential=ManagedIdentityCredential()
)
自定义 blob 内容解析
目前,解析每个 blob 时的默认行为是将内容作为单个 Document 对象返回(使用 UTF-8 编码),无论文件类型如何。对于需要特定解析的文件类型(例如 PDF、CSV 等),或者当你想要控制文档内容格式时,可以提供 loader_factory 参数来接收一个已存在的文档加载器(例如 PyPDFLoader、CSVLoader 等)或自定义加载器。
这通过将 blob 内容下载到临时文件来实现。然后 loader_factory then gets called with the filepath to use the specified document loader to load/parse the file and return the Document 对象。
下面展示如何使用 PyPDFLoader:
from langchain_azure_storage.document_loaders import AzureBlobStorageLoader
from langchain_community.document_loaders import PyPDFLoader # This example requires installing `langchain-community` and `pypdf`
loader = AzureBlobStorageLoader(
"https://<storage-account-name>.blob.core.windows.net",
"<container-name>",
blob_names="<pdf-file.pdf>",
loader_factory=PyPDFLoader,
)
for doc in loader.lazy_load():
print(doc.page_content) # Prints content of each page as a separate document
要提供额外的配置,您可以定义一个可调用对象,该对象返回已实例化的文档加载器,如下所示:
from langchain_azure_storage.document_loaders import AzureBlobStorageLoader
from langchain_community.document_loaders import PyPDFLoader # This example requires installing `langchain-community` and `pypdf`
def loader_factory(file_path: str) -> PyPDFLoader:
return PyPDFLoader(
file_path,
mode="single", # To return the PDF as a single document instead of extracting documents by page
)
loader = AzureBlobStorageLoader(
"https://<storage-account-name>.blob.core.windows.net",
"<container-name>",
blob_names="<pdf-file.pdf>",
loader_factory=loader_factory,
)
for doc in loader.lazy_load():
print(doc.page_content)