访问 AWS Elastic Beanstalk .net 核心应用程序中的 wwwroot 文件夹



在过去的几天里,我一直在努力找出为我的EB .net核心项目存储图像的最佳方式。当我最初开发它时,我只是将图像存储在wwwroot/images/{whatever I needed}目录中。但是,我在部署后不久发现,该项目无权写入(并且可能已读取,尚无办法知道(该文件夹。

我收到的特定错误消息是"Access to the path 'C:\inetpub\AspNetCoreWebApps\app\wwwroot\images' is denied

我尝试过集成AWS的EFS,但我似乎也无法弄清楚。我添加了 AWS EB EFS 文档中提到的storage-efs-mountfilesystem.config文件,但无济于事。我不仅无法访问任何名为/efs的文件夹,我什至不确定这是正确的路径。

关于我看到的唯一可用选项是将所有图像作为 blob 存储在数据库中,我真的宁愿避免这样做。

我尝试在这里使用此答案访问 wwwroot 文件夹,但我得到的响应似乎没有任何变化。

我有合同,需要这个,早点工作,而不是晚点。如果必须,我会走数据库路线,但这几乎不是一个长期的选择。虽然它是一个小型应用程序,并且在可预见的未来将只有一个活跃用户。

文件上传的代码如下:

[Route("Image"), HttpPost()]
public async Task<ResponseInfo> SaveImage()
{
try
{
var file = Request.Form.Files.FirstOrDefault();
if (file != null && file.Length > 0)
{
if (file.Length > _MaxFileSize)
{
return ResponseInfo.Error($"File too large. Max file size is { _MaxFileSize / (1024 * 1024) }MB");
}
var extension = Path.GetExtension(file.FileName).ToLower();
if (extension == ".png" || extension == ".jpg" || extension == ".jpeg" || extension == ".gif")
{
var filename = String.Concat(DateTime.UtcNow.ToString("yyyy-dd-M--HH-mm-ss"), extension);
var basePath = Path.Combine(_Env.WebRootPath, "images", "tmp");
if (Directory.Exists(basePath) == false)
{
Directory.CreateDirectory(basePath);
}
using (var inputStream = new FileStream(Path.Combine(basePath, filename), FileMode.Create))
{
try
{
await file.CopyToAsync(inputStream);
return ResponseInfo.Success(filename);
}
catch (Exception ex)
{
ex.Log();
return ResponseInfo.Error("Failed to save image");
}
}
}
else
{
return ResponseInfo.Error($"File type not accepted. Only PNG, JPG/JPEG, and gif are allowed");
}
}
}
catch (Exception ex) {
return ResponseInfo.Error(ex.Message);
}
return ResponseInfo.Error("No files received.");
}

如果重要的话,我正在运行最新的VS 2017社区。使用AWS Toolkit for Visual Studio 2017 V1.14.4.1发布到 aws 。

我可能会迟到,但这可能有助于其他人。但是,我们无法访问 wwwroot,但我们创建了一个新目录并更改了我们的代码以从该路径访问文件。

我们在项目.ebextensions中添加了一个文件夹,并在该文件夹下添加了一个文件dirAccess.config(名称可以不同(

dirAccess.conifg

commands:
0_:
command: 'if not exist "C:\inetpub\assets" mkdir C:\inetpub\assets'
1_:
command: 'icacls "C:\inetpub\assets" /grant "IIS APPPOOLDefaultAppPool:(OI)(CI)F"'
2_:
command: 'icacls "C:\inetpub\assets"'

请确保此文件夹 (.ebextensions( 包含在发布 zip 中。

若要在发布中包含文件夹,需要将以下行添加到.csproj

<ItemGroup>
<None Include=".ebextensionsalarms.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include=".ebextensionsassetsDirectory.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

相关内容

最新更新