访问 AWS Elastic Beanstalk .net 核心应用程序中的 wwwroot 文件夹 [英] Get access to wwwroot folder in AWS Elastic Beanstalk .net core application

查看:23
本文介绍了访问 AWS Elastic Beanstalk .net 核心应用程序中的 wwwroot 文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

For the last couple days I've been trying to figure out the best way to store images for my EB .net core project. When I initially developed it, I just stored the images in the wwwroot/images/{whatever I needed} directories. However, I found shortly after deployment, that the project does not have permission to write (and probably read, no way of knowing yet) that folder.

我得到的具体错误信息是 访问路径 'C:\inetpub\AspNetCoreWebApps\app\wwwroot\images' 被拒绝

The specific error message I get is "Access to the path 'C:\inetpub\AspNetCoreWebApps\app\wwwroot\images' is denied

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

I've tried integrating AWS's EFS, but I can't seem to figure that out either. I added the storage-efs-mountfilesystem.config file that's mentioned in the AWS EB EFS documentation, but to no avail. Not only do I not have access to any folder named /efs, I'm not even sure that's the right path.

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

About the only available option I see is storing all the images as blobs in the DB, which I'd really rather avoid.

我已尝试使用此答案此处访问 wwwroot 文件夹,但似乎没有我收到的回复有任何变化.

I've tried gaining access to the wwwroot folder using this answer here, but there doesn't seem to be any change in the response I'm getting.

我有合同,需要尽快完成并工作.如果必须的话,我会走 db 路线,但这几乎不是一个长期的选择.虽然它是一个小应用程序,而且在可预见的将来只会有一个活跃用户.

I'm under contract, and need this up and working sooner, rather than later. If I must, I'll go the db route, but it's hardly a long term option. Although it is a small application, and for the foreseeable future will only have one active user.

文件上传的代码以防万一您需要它如下:

The code for the file upload just in case you need it is below:

[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.

If it matters, I'm running the latest VS 2017 Community. Publishing to aws using AWS Toolkit for Visual Studio 2017 V1.14.4.1.

推荐答案

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

I may be late but this might help others. However, we were not able to access the wwwroot but we made a new directory and made changes to our code to access files from that path.

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

We have added a folder to our project .ebextensions and under that we have added a file dirAccess.config (name can be different)

dirAccess.conifg

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 中.

Please make sure that this folder (.ebextensions) is included in the publish zip.

要将文件夹包含在发布中,需要在.csproj

To include the folder in publish, need to add below lines to .csproj

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

这篇关于访问 AWS Elastic Beanstalk .net 核心应用程序中的 wwwroot 文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆