跳至主要內容

AzureBlobFilesystem

將檔案儲存在 Azure Blob Storage 容器中。介面詳情請參閱 WorkspaceFilesystem 介面

安裝
安裝 的直接連結

npm install @mastra/azure

用法
用法 的直接連結

AzureBlobFilesystem 加入 Workspace,並指派給 Agent:

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,
})

帳戶金鑰
帳戶金鑰 的直接連結

如果不想傳入完整連接字串,請使用帳戶名稱和帳戶金鑰:

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
共用存取簽章 token 的直接連結

將共用存取簽章(SAS)token 與 accountNameendpoint 配合使用:

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
DefaultAzureCredential 的直接連結

當你的環境提供 Azure 身分憑證時,請使用 DefaultAzureCredential

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 install @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
= false
使用 @azure/identity 的 DefaultAzureCredential。

endpoint?:

string
自訂 Blob 服務端點 URL。使用 Azurite 進行本機開發時使用。

prefix?:

string
所有 key 的可選前綴(作用類似子目錄)。

id?:

string
= 自動產生
此檔案系統執行個體的唯一標識符。

displayName?:

string
在 UI 中顯示、便於閱讀的名稱。

icon?:

FilesystemIcon
UI 的圖示標識符。

description?:

string
此檔案系統在 UI 中顯示的簡短說明。

readOnly?:

boolean
= false
設為 true 時,所有寫入操作都會被封鎖。

屬性
屬性 的直接連結

id:

string
檔案系統執行個體標識符。

name:

string
Provider 名稱('AzureBlobFilesystem')。

provider:

string
Provider 標識符('azure-blob')。

readOnly:

boolean | undefined
檔案系統是否處於唯讀模式。

方法
方法 的直接連結

AzureBlobFilesystem 實作了 WorkspaceFilesystem 介面,並提供所有標準檔案系統方法:

  • 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()
init 的直接連結

初始化檔案系統。驗證容器存取權及憑證。

await filesystem.init()

getInfo()
getinfo 的直接連結

傳回此檔案系統執行個體的元資料。

const info = filesystem.getInfo()
// { id: '...', name: 'AzureBlobFilesystem', provider: 'azure-blob', status: 'ready' }

getContainer()
getcontainer 的直接連結

傳回底層 Azure Blob Storage ContainerClient

const container = await filesystem.getContainer()

getMountConfig()
getmountconfig 的直接連結

傳回支援 Azure Blob 檔案系統掛載之 Sandbox Provider 的掛載配置。

const config = filesystem.getMountConfig()
// { type: 'azure-blob', container: 'my-container', ... }
備註

Azure Blob Sandbox 掛載取決於 Sandbox 是否支援 azure-blob 掛載配置。如果你的 Sandbox Provider 不支援 Azure Blob 掛載,請使用 filesystem 直接執行 Workspace 檔案操作。