Class AzureBlobFileStorage
java.lang.Object
io.leandev.appfuse.file.azure.AzureBlobFileStorage
- All Implemented Interfaces:
FileStorage
Azure Blob Storage 檔案儲存實作
將檔案儲存在 Azure Blob Storage。適用於 Azure 雲端部署或需要分散式儲存的場景。 此類別是純 Java 實作,不依賴 Spring。應用層需透過 Builder 建立並註冊為 Spring Bean。
Blob 名稱結構
{container}/
├── {blobPrefix}/staging/ # 暫存區
│ └── {tenantId}/
│ └── {date}/{uuid} # 暫存檔案
└── {blobPrefix}/files/ # 永久區
└── {tenantId}/
└── {yyyy-MM-dd}/{HH}/
└── {filename-shortUuid}.{ext}
使用方式
// 建立 BlobServiceClient(使用 Connection String)
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
.connectionString(connectionString)
.buildClient();
// 建立 AzureBlobFileStorage
FileStorage fileStorage = AzureBlobFileStorage.builder()
.blobServiceClient(blobServiceClient)
.container("appfuse-files")
.blobPrefix("myapp")
.sasUploadEnabled(true)
.sasDownloadEnabled(true)
.sasTokenExpiration(3600)
.maxFileSize(50 * 1024 * 1024) // 50MB,選填
.cdnUrl("https://cdn.example.com") // 選填,CDN URL
.build();
檔案大小限制
可選的 maxFileSize 參數用於限制檔案大小。設定後會在 prepareStaging 和 store 時檢查。 若未設定(值為 0 或負數),則不進行大小限制。
getUrl() 行為
getUrl(String, String) 方法的回傳值依設定而定:
- 若 sasDownloadEnabled=true:回傳 SAS URL
- 若有設定 cdnUrl:回傳 CDN URL
- 否則:拋出
UnsupportedOperationException
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classAzure Blob Storage 例外static classAzureBlobFileStorage 的建構器Nested classes/interfaces inherited from interface FileStorage
FileStorage.FileMetadata, FileStorage.StagingUploadInfo -
Method Summary
Modifier and TypeMethodDescriptionstatic AzureBlobFileStorage.Builderbuilder()建立 Builder 實例voidcompleteStagingUpload(String tenantId, String tempId, InputStream content, long size) 完成暫存上傳 - 統一上傳流程的第二步void刪除永久區檔案voiddeleteStaged(String tenantId, String tempId) 刪除暫存檔案boolean檢查永久區檔案是否存在booleanexistsStaged(String tenantId, String tempId) 檢查暫存檔案是否存在getInputStream(String tenantId, String fileId) 取得永久區檔案的 InputStreamgetMetadata(String tenantId, String fileId) 取得永久區檔案的 MetadatagetStagedInputStream(String tenantId, String tempId) 取得暫存檔案的 InputStreamgetStagedMetadata(String tenantId, String tempId) 取得暫存檔案的 Metadata取得儲存類型名稱取得檔案的存取 URLboolean檢查是否啟用 SAS 上傳將暫存檔案持久化到永久區prepareStaging(String tenantId, String filename, String contentType, long size) 準備暫存上傳 - 統一上傳流程的第一步store(String tenantId, String filename, InputStream content, String contentType, long size) 直接儲存檔案到永久區(跳過暫存)
-
Method Details
-
builder
-
prepareStaging
public FileStorage.StagingUploadInfo prepareStaging(String tenantId, String filename, String contentType, long size) Description copied from interface:FileStorage準備暫存上傳 - 統一上傳流程的第一步
前端先呼叫此方法取得上傳 URL,再透過 PUT 上傳檔案內容。 此設計讓所有儲存類型(Local、Database、S3)使用統一的前端上傳邏輯。
- Specified by:
prepareStagingin interfaceFileStorage- Parameters:
tenantId- 租戶 IDfilename- 原始檔名contentType- MIME 類型size- 檔案大小- Returns:
- StagingUploadInfo(含 tempId 和上傳 URL)
-
completeStagingUpload
Description copied from interface:FileStorage完成暫存上傳 - 統一上傳流程的第二步
接收 PUT 請求的檔案內容,完成暫存區上傳。 此方法由 Controller 在收到 PUT 請求時呼叫。
- Specified by:
completeStagingUploadin interfaceFileStorage- Parameters:
tenantId- 租戶 IDtempId- 暫存檔案 ID(由 prepareStaging 回傳)content- 檔案內容的 InputStreamsize- 檔案大小
-
getStagedMetadata
Description copied from interface:FileStorage取得暫存檔案的 Metadata- Specified by:
getStagedMetadatain interfaceFileStorage- Parameters:
tenantId- 租戶 IDtempId- 暫存檔案 ID- Returns:
- 檔案 Metadata,若不存在則回傳 empty
-
getStagedInputStream
public InputStream getStagedInputStream(String tenantId, String tempId) throws FileNotFoundException Description copied from interface:FileStorage取得暫存檔案的 InputStream- Specified by:
getStagedInputStreamin interfaceFileStorage- Parameters:
tenantId- 租戶 IDtempId- 暫存檔案 ID- Returns:
- InputStream(呼叫端負責關閉)
- Throws:
FileNotFoundException- 若檔案不存在
-
existsStaged
Description copied from interface:FileStorage檢查暫存檔案是否存在- Specified by:
existsStagedin interfaceFileStorage- Parameters:
tenantId- 租戶 IDtempId- 暫存檔案 ID- Returns:
- 是否存在
-
deleteStaged
Description copied from interface:FileStorage刪除暫存檔案- Specified by:
deleteStagedin interfaceFileStorage- Parameters:
tenantId- 租戶 IDtempId- 暫存檔案 ID
-
persist
Description copied from interface:FileStorage將暫存檔案持久化到永久區
自動生成唯一的 fileId,格式為
yyyy-MM-dd/HH/filename-shortUuid.ext。 業務層只需儲存回傳的 fileId,不需關心檔案實際儲存位置。- Specified by:
persistin interfaceFileStorage- Parameters:
tenantId- 租戶 IDtempId- 暫存檔案 ID- Returns:
- fileId(唯一識別碼,同時也是可讀的路徑格式)
-
store
public String store(String tenantId, String filename, InputStream content, String contentType, long size) Description copied from interface:FileStorage直接儲存檔案到永久區(跳過暫存)- Specified by:
storein interfaceFileStorage- Parameters:
tenantId- 租戶 IDfilename- 原始檔名(用於生成 fileId)content- 檔案內容的 InputStreamcontentType- MIME 類型size- 檔案大小- Returns:
- fileId
-
getMetadata
Description copied from interface:FileStorage取得永久區檔案的 Metadata- Specified by:
getMetadatain interfaceFileStorage- Parameters:
tenantId- 租戶 IDfileId- 檔案 ID- Returns:
- 檔案 Metadata,若不存在則回傳 empty
-
getInputStream
Description copied from interface:FileStorage取得永久區檔案的 InputStream- Specified by:
getInputStreamin interfaceFileStorage- Parameters:
tenantId- 租戶 IDfileId- 檔案 ID- Returns:
- InputStream(呼叫端負責關閉)
- Throws:
FileNotFoundException- 若檔案不存在
-
exists
Description copied from interface:FileStorage檢查永久區檔案是否存在- Specified by:
existsin interfaceFileStorage- Parameters:
tenantId- 租戶 IDfileId- 檔案 ID- Returns:
- 是否存在
-
delete
Description copied from interface:FileStorage刪除永久區檔案- Specified by:
deletein interfaceFileStorage- Parameters:
tenantId- 租戶 IDfileId- 檔案 ID
-
getUrl
取得檔案的存取 URL
優先順序:SAS URL > CDN URL > 拋出例外
- Specified by:
getUrlin interfaceFileStorage- Parameters:
tenantId- 租戶 IDfileId- 檔案 ID- Returns:
- 存取 URL(僅雲端儲存支援)
- Throws:
UnsupportedOperationException- 若未啟用 SAS URL 且未設定 CDN URL
-
getStorageType
Description copied from interface:FileStorage取得儲存類型名稱- Specified by:
getStorageTypein interfaceFileStorage- Returns:
- 儲存類型(例如 "local", "azure", "s3", "database", "sftp")
-
isSasUploadEnabled
public boolean isSasUploadEnabled()檢查是否啟用 SAS 上傳- Returns:
- 若啟用 SAS 上傳則回傳 true
-