ABP.IO 使用 Azure Storage 上傳圖片
筆記下 ABP Storage 用法與上傳圖片相關資訊
Blob Storing Azure
首先參照官方文件安裝並設定 Azure Storage
Blob Storing Azure | Documentation Center | ABP.IO
我是把 volo.abp.blobstoring.azure 套件裝在 Domain 層
然後把 Azure Storage 連接字串設定在 HttpApi.Host (Web)
Save
基本用法請參考以下文件
範例
SaveImagesAsync (Application)
如果不是直接輸入 IFormFile 則要記得加
[FromForm]SaveAsync可以輸入 streamOpenReadStream記得 using 不然可能要自己 close ?
SaveImageDto (Application.Contracts)
IValidatableObject會自動驗證輸入資料,需實作 Validate Validation | Documentation Center | ABP.IOValidationResult第一個參數是 message,可以自己調整StorageHelper這個類要自己建出來,如下所示
Application.Contracts.Helpers.StorageHelper.cs
檢查
ContentType或是副檔名是否合乎規定如果不要 .gif 可以自己從
formats拿掉
Get
fs.Position = 0;=> 不加這行會不能下載,是不是 Bug 我不知道,但絕對是個坑! How to user IRemoteStreamContent with BLOB Storing Azure Provider · Issue #7418 · abpframework/abp (github.com)IContentTypeProvider=> 這邊DIFileExtensionContentTypeProvider來判斷 ContentType
如果不管 ContentType 那就改成以下這樣也就不用設定 DI 了
return new RemoteStreamContent(fs,name,"application/octet-stream");
參照
Application Services | Documentation Center | ABP.IO
File Upload/Download with BLOB Storage System in ASP.NET Core & ABP Framework | ABP Community
備註
如果無法儲存可能要去 Azure 手動建立 container
名稱需與設定一致 azure.ContainerName = "your azure container name";
單元測試
剛測試發現整合測試其他 API 過不了
到 TestBase 設定 DI 可以先正常測試
test\YourProject.TestBase.YourProjectTestBaseModule.cs
// TODO: context.Services.Replace(ServiceDescriptor.Singleton<IBlobContainer, NullBlobContainer>());

PS5
ABP
回首頁
本文章從點部落遷移至 Writerside