0

Net core application. I am trying to upload file to data lake through API. I have below controller method which accepts file.

 [HttpPost]
        public async Task<IActionResult> Upload(IFormFile files)
        {
            var path = Path.Combine(Directory.GetCurrentDirectory(), "Uploads", files.FileName);
            var stream = new FileStream(path, FileMode.Create);
            string containerName = "raw";
            DataLakeServiceClient dataLakeServiceClient = _dataLakeRepository.GetDataLakeServiceClient("test");
            DataLakeFileSystemClient dataLakeFileSystemClient = _dataLakeRepository.GetFileSystem(dataLakeServiceClient, containerName);
            await _dataLakeRepository.UploadFile(dataLakeFileSystemClient, "directory2", "text1.txt", stream);
            return Ok();
        }

I have below DataLake method which will upload file to data lake.

 public async Task UploadFile(DataLakeFileSystemClient fileSystemClient, string directoryName, string fileName, Stream content)
        {
            DataLakeDirectoryClient directoryClient = fileSystemClient.GetDirectoryClient(directoryName);

            DataLakeFileClient fileClient = await directoryClient.CreateFileAsync(fileName);

            long fileSize = content.Length;

            await fileClient.AppendAsync(content, offset: 0);

            await fileClient.FlushAsync(position: fileSize);
        }

Below method to get file system client

public DataLakeFileSystemClient GetFileSystem(DataLakeServiceClient serviceClient, string FileSystemName)
        {
            return serviceClient.GetFileSystemClient(FileSystemName);
        }

I tried to upload file and In below line

await fileClient.AppendAsync(content, offset: 0);

I got below error

Azure.RequestFailedException: The value for one of the HTTP headers is not in the correct format.
Status: 400 (The value for one of the HTTP headers is not in the correct format.)
ErrorCode: InvalidHeaderValue

Also when I debug I see content.Length is also zero. I think I am missing something in stream because I am having some issue with stream. I am not able to figure out the issue. Can someone help me to fix this. Any help would be appreciated. Thanks

Mr Perfect
  • 585
  • 8
  • 30

1 Answers1

0

After read official doc, we can find the sample code use FileStream.

So you should convert Stream to FileStream .

enter image description here

Jason Pan
  • 15,263
  • 1
  • 14
  • 29