この記事では、オープン ソースの取得拡張生成 (RAG) ツールで使用するために、Azure ファイル共有に対して認証を行い、その内容をダウンロードする方法について説明します。
前提条件
- クエリするドキュメントを含む Azure ファイル共有。 Azureサブスクリプションをお持ちでない場合は、無料で作成。
- Python 3.12.10。 Windowsで、x64 バージョンをインストールします。
- Azure CLI。
Azure ファイル共有へのアクセスを許可する
この記事では、Azure ソフトウェア開発キット (SDK) に推奨される資格情報パターンである DefaultAzureCredential によるMicrosoft Entra ID認証を使用します。 この方法では、ストレージ アカウント キーを回避し、開発環境と運用環境全体で機能する移植可能な認証メカニズムを提供します。
ヒント
通常、 403 Forbidden エラーを受信すると、認証に失敗するのではなく、承認が不足していることが示されます。
ファイル共有をホストするストレージ アカウントにストレージ ファイル データ特権閲覧者 ロールを割り当てます。
注
コードが token_intent="backup" を使用してAzure Filesにアクセスするため、このロールが必要です。 このアクセス パターンではファイル レベルのアクセス許可がバイパスされるため、Azureには特権ロールが必要です。 コードは読み取り操作のみを実行し、ファイルの内容を変更しないため、 ストレージ ファイル データの特権閲覧者 ロールで十分です。
Azure portal
- Azure ポータルにサインインし、ストレージ アカウントに移動します。
- Access Control (IAM)>Add>ロールの割り当ての追加を選択します。
- Storage File Data Privileged Reader を検索して選択し、[次へ] を選択します。
- [ メンバーの選択] を選択し、ユーザー アカウントを検索して選択します。
- レビュー + 割り当て を選択します。
Azure CLI
az login
az role assignment create \
--assignee $(az ad signed-in-user show --query id -o tsv) \
--role "Storage File Data Privileged Reader" \
--scope $(az storage account show \
--name <your-storage-account-name> \
--query id -o tsv)
Azure PowerShell
Connect-AzAccount
New-AzRoleAssignment `
-SignInName (Get-AzADUser -SignedIn).UserPrincipalName `
-RoleDefinitionName "Storage File Data Privileged Reader" `
-Scope (Get-AzStorageAccount `
-ResourceGroupName <your-resource-group> `
-Name <your-storage-account-name>).Id
環境変数の設定
Azure Files接続の詳細を含む .env ファイルをプロジェクト ディレクトリに作成します。
AZURE_STORAGE_ACCOUNT_NAME=<your-storage-account-name>
AZURE_STORAGE_SHARE_NAME=<your-share-name>
| Variable | 説明 |
|---|---|
AZURE_STORAGE_ACCOUNT_NAME |
Azure Storage アカウントの名前 |
AZURE_STORAGE_SHARE_NAME |
Azure ファイル共有の名前 |
Azure ファイル共有からファイルをダウンロードする
必要なパッケージをインストールします。
-
azure-identity—パスワードレス認証のDefaultAzureCredentialを提供します。 -
azure-storage-file-share— 共有への接続と共有からのファイルのダウンロードに使用するShareClientを提供します。
pip install azure-identity pip install azure-storage-file-share-
Azure ファイル共有に接続し、そのディレクトリ構造を再帰的に列挙し、各ファイルの検索とダウンロードに必要な詳細を収集します。 Microsoft Entra ID ベースの認証を使用する場合、
ShareClientにはtoken_intent="backup"が必要です。import os import posixpath import tempfile from azure.identity import DefaultAzureCredential from azure.storage.fileshare import ShareClient account_name = os.environ["AZURE_STORAGE_ACCOUNT_NAME"] share_name = os.environ["AZURE_STORAGE_SHARE_NAME"] share = ShareClient( account_url=f"https://{account_name}.file.core.windows.net", share_name=share_name, credential=DefaultAzureCredential(), token_intent="backup", ) root = share.get_directory_client("") file_references = [] directories_to_traverse = [root] while directories_to_traverse: current = directories_to_traverse.pop() for item in current.list_directories_and_files(): if item.is_directory: directories_to_traverse.append(current.get_subdirectory_client(item.name)) else: # Azure Files paths use posix-style separators. relative_path = posixpath.join(current.directory_path or "", item.name) file_references.append((item.name, relative_path, current))ファイルをダウンロードします。 ファイルをディスクに書き込む前に、コードは解決された各ファイル パスを検証して、ターゲット ディレクトリ内に残っていることを確認します。 この検証により、外部ソースからディレクトリ構造を処理するときに、ファイルが意図した場所の外部に書き込まれるのを防ぐことができます。
with tempfile.TemporaryDirectory() as destination: for name, relative_path, parent_directory in file_references: file_client = parent_directory.get_file_client(name) local_path = os.path.join(destination, relative_path) # Path traversal guard real_dest = os.path.realpath(destination) + os.sep if not os.path.realpath(local_path).startswith(real_dest): raise ValueError(f"Path traversal detected: {relative_path}") os.makedirs(os.path.dirname(local_path), exist_ok=True) with open(local_path, "wb") as f: for chunk in file_client.download_file().chunks(): f.write(chunk)
次のステップ
解析、チャンク、埋め込み、およびクエリを続行するチュートリアルを選択します。
- LangChain - LangChain + Pinecone、Weaviate、Qdrant
- LlamaIndex - LlamaIndex + Pinecone、Weaviate、Qdrant
- Haystack - Haystack + Pinecone、Weaviate、Qdrant