> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-HK/llms.txt # AzureBlobFilesystem 將檔案儲存在 Azure Blob Storage 容器中。介面詳情請參閱 [WorkspaceFilesystem 介面](https://mastra.zisheng.pro/zh-HK/reference/workspace/filesystem)。 ## 安裝 **npm**: ```bash npm install @mastra/azure ``` **pnpm**: ```bash pnpm add @mastra/azure ``` **Yarn**: ```bash yarn add @mastra/azure ``` **Bun**: ```bash bun add @mastra/azure ``` ## 用法 將 `AzureBlobFilesystem` 加入 Workspace,並指派給 Agent: ```typescript import { Agent } from '@mastra/core/agent' import { Workspace } from '@mastra/core/workspace' import { AzureBlobFilesystem } from '@mastra/azure/blob' const workspace = new Workspace({ filesystem: new AzureBlobFilesystem({ container: 'my-container', connectionString: process.env.AZURE_STORAGE_CONNECTION_STRING, }), }) const agent = new Agent({ id: 'file-agent', name: 'file-agent', model: 'anthropic/claude-opus-4-7', workspace, }) ``` ### 帳戶金鑰 如果不想傳入完整連接字串,請使用帳戶名稱和帳戶金鑰: ```typescript import { AzureBlobFilesystem } from '@mastra/azure/blob' const filesystem = new AzureBlobFilesystem({ container: 'my-container', accountName: process.env.AZURE_STORAGE_ACCOUNT_NAME, accountKey: process.env.AZURE_STORAGE_ACCOUNT_KEY, }) ``` ### 共用存取簽章 token 將共用存取簽章(SAS)token 與 `accountName` 或 `endpoint` 配合使用: ```typescript import { AzureBlobFilesystem } from '@mastra/azure/blob' const filesystem = new AzureBlobFilesystem({ container: 'my-container', accountName: process.env.AZURE_STORAGE_ACCOUNT_NAME, sasToken: process.env.AZURE_STORAGE_SAS_TOKEN, }) ``` ### DefaultAzureCredential 當你的環境提供 Azure 身分憑證時,請使用 `DefaultAzureCredential`: ```typescript import { AzureBlobFilesystem } from '@mastra/azure/blob' const filesystem = new AzureBlobFilesystem({ container: 'my-container', accountName: process.env.AZURE_STORAGE_ACCOUNT_NAME, useDefaultCredential: true, }) ``` 使用 `DefaultAzureCredential` 時,請安裝 `@azure/identity`: **npm**: ```bash npm install @azure/identity ``` **pnpm**: ```bash pnpm add @azure/identity ``` **Yarn**: ```bash yarn add @azure/identity ``` **Bun**: ```bash bun add @azure/identity ``` ## 建構函數參數 **container** (`string`): Azure Blob Storage 容器名稱 **connectionString** (`string`): 完整 Azure Storage 連接字串。優先於其他驗證選項。 **accountName** (`string`): Azure Storage 帳戶名稱。除非使用 connectionString 或 endpoint,否則必須提供。 **accountKey** (`string`): Azure Storage 帳戶金鑰。 **sasToken** (`string`): 共用存取簽章 token。必須配合 accountName 或 endpoint 使用。 **useDefaultCredential** (`boolean`): 使用 @azure/identity 的 DefaultAzureCredential。 (Default: `false`) **endpoint** (`string`): 自訂 Blob 服務端點 URL。使用 Azurite 進行本機開發時使用。 **prefix** (`string`): 所有 key 的可選前綴(作用類似子目錄)。 **id** (`string`): 此檔案系統執行個體的唯一標識符。 (Default: `自動產生`) **displayName** (`string`): 在 UI 中顯示、便於閱讀的名稱。 **icon** (`FilesystemIcon`): UI 的圖示標識符。 **description** (`string`): 此檔案系統在 UI 中顯示的簡短說明。 **readOnly** (`boolean`): 設為 true 時,所有寫入操作都會被封鎖。 (Default: `false`) ## 屬性 **id** (`string`): 檔案系統執行個體標識符。 **name** (`string`): Provider 名稱('AzureBlobFilesystem')。 **provider** (`string`): Provider 標識符('azure-blob')。 **readOnly** (`boolean | undefined`): 檔案系統是否處於唯讀模式。 ## 方法 AzureBlobFilesystem 實作了 [WorkspaceFilesystem 介面](https://mastra.zisheng.pro/zh-HK/reference/workspace/filesystem),並提供所有標準檔案系統方法: - `readFile(path, options?)` - 讀取檔案內容 - `writeFile(path, content, options?)` - 將內容寫入檔案 - `appendFile(path, content)` - 將內容附加至檔案 - `deleteFile(path, options?)` - 刪除檔案 - `copyFile(src, dest, options?)` - 複製檔案 - `moveFile(src, dest, options?)` - 移動檔案或重新命名 - `mkdir(path, options?)` - 建立目錄 - `rmdir(path, options?)` - 移除目錄 - `readdir(path, options?)` - 列出目錄內容 - `exists(path)` - 檢查路徑是否存在 - `stat(path)` - 取得檔案或目錄的元資料 ### `init()` 初始化檔案系統。驗證容器存取權及憑證。 ```typescript await filesystem.init() ``` ### `getInfo()` 傳回此檔案系統執行個體的元資料。 ```typescript const info = filesystem.getInfo() // { id: '...', name: 'AzureBlobFilesystem', provider: 'azure-blob', status: 'ready' } ``` ### `getContainer()` 傳回底層 Azure Blob Storage `ContainerClient`。 ```typescript const container = await filesystem.getContainer() ``` ### `getMountConfig()` 傳回支援 Azure Blob 檔案系統掛載之 Sandbox Provider 的掛載配置。 ```typescript const config = filesystem.getMountConfig() // { type: 'azure-blob', container: 'my-container', ... } ``` > **備註:** Azure Blob Sandbox 掛載取決於 Sandbox 是否支援 `azure-blob` 掛載配置。如果你的 Sandbox Provider 不支援 Azure Blob 掛載,請使用 `filesystem` 直接執行 Workspace 檔案操作。 ## 相關內容 - [WorkspaceFilesystem 介面](https://mastra.zisheng.pro/zh-HK/reference/workspace/filesystem) - [S3Filesystem 參考](https://mastra.zisheng.pro/zh-HK/reference/workspace/s3-filesystem) - [GCSFilesystem 參考](https://mastra.zisheng.pro/zh-HK/reference/workspace/gcs-filesystem) - [Workspace 概覽](https://mastra.zisheng.pro/zh-HK/docs/workspace/overview)