asp.net 核心存储图像 wwwroot docker

asp.net core store images wwwroot docker

我有一个 .net 6 网站 api。

当用户上传图片时,它应该存储在 wwwroot 文件夹中。

当我 运行 docker 我从后端收到此错误:

backend    | Unhandled exception. System.IO.DirectoryNotFoundException: /wwwroot\images/
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 中是短暂的。上传到磁盘只是暂时的。您应该上传到云存储系统(azure blob、AWS S3 等)并将 URL 保存在数据库中的某个地方。