8

I am currently building a C# WebApi 2 application that I will be uploading to an Amazon Elastic Beanstalk instance to deploy. I am having success so far, and on my local machine, I just finished testing the file upload capability in order for clients to upload images.

The way it goes is I accept the multipart/formdata in the Web Api and save the temp file (with a random name like BodyPart_24e246c7-a92a-4a3d-84ef-c1651416e667) to the App_Data folder. The temporary file is put into an S3 Bucket and I create a reference in my SQL Server database to it.

Testing works fine with single or multiple file uploads locally but when I deploy the application to Elastic Beanstalk and try to upload I get errors like "Could not find a part of the path 'C:\inetpub\wwwroot\sbeAPI_deploy\App_Data\BodyPart_8f552d48-ed9b-4ec2-9986-88cbffd673ee'" or a similar one saying access is denied altogether.

I have been trying to find the solution online for a few hours now, but the AWS documentation is all over the place and tutorials/other questions seem to be outdated. I believe it has something to do with not having permission to write the temporary files on the EC2 server, but I can't figure out how to fix it.

Thanks very much in advance.

Elie Zeitouni
  • 251
  • 1
  • 4
  • 13
  • Facing the same issue, did you ever resolve this? I suspect there is a way to upload EC2 specific config. – DivanMoller Oct 14 '14 at 06:14
  • 1
    I'm afraid not :( this problem and acouple others led us to switch to Microsoft Azure, which ended up being better suited to .NET applications. – Elie Zeitouni Oct 29 '14 at 16:56

5 Answers5

5

This is already possible since April 2013, see also here: Basically the steps you need to perform are the following:

  1. Create a folder called .ebextensions in the top-level of your project through the solution explorer
  2. Add in this folder your configuration file e.g myapp.config (replace myapp with your Elastic Beanstalk's app name)
  3. Add the code displayed underneath to this configuration file you just created. Replace MyApp with your project name (not solution name) displayed in Visual Studio
  4. All set!! Be sure there's a file within App_Data otherwise Visual Studio won't publish it.

    {
        "containercommands": {
            "01-changeperm": {
                "command": "icacls \"C:/inetpub/wwwroot/MyApp_deploy/App_Data\" /grant DefaultAppPool:(OI)(CI)"
            }
        }
    }
    
Mark Amery
  • 143,130
  • 81
  • 406
  • 459
bicycle
  • 8,315
  • 9
  • 52
  • 72
  • How do you access it from WebAPI? AppDomain.CurrentDomain.BaseDirectory + @"App_Data\"; Seems to give 'C:\inetpub\wwwroot\App_Data'. It is still throwing access denied errors. I tried changing the config file to point to 'C:\inetpub\wwwroot\App_Data' to no avail. – Dasith Wijes Dec 13 '16 at 15:55
  • I'm trying to do this and it is not working - any tip? – BLuM Mar 13 '17 at 14:19
  • 1
    The "containercommands" is now "container_commands" and "01-changeperm" is "01-aclchange".See http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/ebextensions.html – jamos Oct 20 '17 at 18:39
  • I'm having some issues with this as well. Running your suggestion didn't seem to yield any results. Are you still available to help out? Here's my information https://stackoverflow.com/questions/52028876/get-access-to-wwwroot-folder-in-aws-elastic-beanstalk-net-core-application – Nieminen Aug 28 '18 at 00:36
  • How to create this ``myapp.config`` file (using VS 2019 | .net core 3.1) but it takes this file as xml with this extension and can't let me put this code inside it. – Dave Apr 21 '21 at 10:13
3

To give write permission to your DefaultAppPool you can
create an .ebextensions folder
create a config file and place it in your .ebextensions folder

This will change permission to your wwwroot folder

container_commands:
 01-changeperm :
  command : 'icacls "C:\\inetpub\\wwwroot" /grant "IIS APPPOOL\DefaultAppPool:(OI)(CI)F"'
TResponse
  • 3,940
  • 7
  • 43
  • 63
ynot
  • 31
  • 1
2

I had the same problem (unable to write to a file in the App_Data folder of my web application on Elastic Beanstalk).

In my case it was sufficient to create a dummy file in the App_Data folder in my Visual Studio project. When I did this, the App_Data folder was created during deployment with permissions that allow the web application to write to it.

No need for .ebextensions to change folder permissions.

Joe
  • 122,218
  • 32
  • 205
  • 338
1

The App_Data folder does not have write permissions by default, and you would have to set appropriate permissions during deployment of your apps.

Check out this post for a detailed explanation of how to do it: http://thedeveloperspace.com/granting-write-access-to-asp-net-apps-hosted-on-aws-beanstalk/

Shameel
  • 632
  • 5
  • 12
0

This question is pretty old but for anyone else who ends up having the same issue. I had the same issue with AWS. Connect to your instance and change the properties for the folder you want to upload files to. Select the folder you want to grant read/write access to. Click on properties and set the permissions that way.

My issue was with uploading images to the server. I couldn't put it in the App_Data folder since that is a special offer reserved for the app only and I needed the images to be accessible through the URL. So I created another folder "Uploads". Published my api then connected to the instance through remote desktop. Located the Content folder, and set the properties to read/write for DefaultAppPool. That solved my problem, hope this helps someone out there.

  • this is for elasticbeanstalk. you don't have ec2 access by default and defeats the purpose in an autoscaling environment. – Eric Kelly Aug 06 '18 at 03:11