Home > Software engineering >  I can create folders, but cannot write files to them
I can create folders, but cannot write files to them

Time:04-25

I am currently writing an Asp.Net / Angular app which should read PDFs from and write to a Windows net drive. The app is hosted in a Linux Docker container on a Linux Server. The app can create folders, but as soon as the PDF should be saved on the drive, I get an UnauthorizedAccessException:

System.UnauthorizedAccessException: Access to the path '/path/to/file/document.pdf' is denied.
 ---> System.IO.IOException: Permission denied

I am mounting the net drive inside a docker-compose.yml like this:

version: '3'

services:
    MyService:
        image: registry.my-registry.de/group/my-service:latest
        privileged: true
        ports:
        - "9191:443"
        restart: always
        environment:
        - ASPNETCORE_ENVIRONMENT=Production
        - ASPNETCORE_URLS=https:// :443
        - ASPNETCORE_Kestrel__Certificates__Default__Password=
        - ASPNETCORE_Kestrel__Certificates__Default__Path=/https/aspnetapp.pfx
        volumes:
        - ./ssl/my-certificate.p12:/https/aspnetapp.pfx
        - my-volume:/path/to/file:rw
volumes:
    my-volume:
        driver_opts:
            type: cifs
            o: username=MyUser,password=password,domain=MyDomain,rw,iocharset=utf8,file_mode=0777,dir_mode=0777
            device: "//hostip/path/to/targetfolder"

MyUser is an AD account and has read-write permissions on the targetfolder. When I go inside the docker terminal via a bash terminal and navigate to the mounted drive, I am able to create a file and some text to it.

The method, which writes the document to the drive, looks like this:

[HttpPost]
public async Task<ActionResult<File>> CreateAsync([FromForm] IFormFile file) // file was sent from the Angular front end
{
/*...*/
    using (var stream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
    {
        await file.CopyToAsync(stream); //Exception occurs here
    }
/*...*/
}

This code works inside my test environment (CentOS 7) without any issues. Am I missing something?

CodePudding user response:

Okay, I tried something and it worked. Instead of using CopyToAsync I converted the file to a byte array and then wrote it to disk:

using (var ms = new MemoryStream())
using (var fs = System.IO.File.Create(filePath))
{
    await file.CopyToAsync(ms);
    await fs.WriteAsync(ms.ToArray());
}

I have no idea why this works, but this took me days to figure out, so I'll take it.

  • Related