Get access to wwwroot folder in AWS Elastic Beanst

2019-07-28 21:30发布

问题:

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.