Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get access to wwwroot folder in AWS Elastic Beanstalk .net core application

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.

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

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.

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

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.

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.");
    }

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.

like image 653
Nieminen Avatar asked Oct 23 '25 16:10

Nieminen


1 Answers

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.

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

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

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

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

  <ItemGroup>
    <None Include=".ebextensions\alarms.config">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    <None Include=".ebextensions\assetsDirectory.config">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>
like image 95
user5826312 Avatar answered Oct 26 '25 09:10

user5826312



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!