我有一个.net 6 web api。
当用户上传图像时,它应该存储在wwwroot文件夹中。
当我运行docker时,我从后端收到了这个错误:
backend | Unhandled exception. System.IO.DirectoryNotFoundException: /wwwrootimages/
backend | at Microsoft.Extensions.FileProviders.PhysicalFileProvider..ctor(String root, ExclusionFilters filters)
backend | at Microsoft.Extensions.FileProviders.PhysicalFileProvider..ctor(String root)
backend | at Program.<Main>$(String[] args) in /RealEstateAPI/Program.cs:line 65
backend exited with code 139
这是我的Docker文件:
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /
COPY RealEstateAPI/appsettings.json ./RealEstateAPI/AppSettings.json
COPY RealEstateAPI/appsettings.Development.json ./RealEstateAPI/AppSettings.Development.json
COPY RealEstateAPI/wwwroot ./RealEstateAPI/
COPY . .
RUN dotnet restore "RealEstateAPI/RealEstateAPI.csproj"
COPY . .
WORKDIR "/RealEstateAPI"
RUN dotnet build "RealEstateAPI.csproj" -c Release -o /build
FROM build AS publish
RUN dotnet publish "RealEstateAPI.csproj" -c Release -o /publish
FROM base AS final
WORKDIR /
COPY --from=publish /publish .
ENTRYPOINT ["dotnet", "RealEstateAPI.dll"]
如何创建wwwRoot卷并存储用户在运行时上传的图像?
- 音量将在Heroku中,因为docker图像也将在那里
文件系统在Heroku中是短暂的。上传到磁盘将只是暂时的。您应该上传到云存储系统(azure blobs、AWS S3等(,并将URL保存在某个数据库中。