Search code examples
azurerestfilestreamazure-data-lake

how to pass Body for ADLS Gen-2 as a stream


I'm Using ADLS Gen2 Path-Update API to update file from ADLS which is already created. As a body i can easily pass string which is working fine but the same with Stream is not working.

i'm reading local file data and trying to store it into stream and pass as a body but getting an error of Http request header is invalid


Solution

  • I have a quick test at my side, the following code which read local file as stream, then upload the stream to adls gen2. It works fine. Please try it at your side, and let me know if you have more issues.

            static void Main(string[] args)
            {                
                var auth = new AzureServiceTokenProvider();          
    
                const string url = "https://storage.azure.com/";
                string token = auth.GetAccessTokenAsync(url).Result;
    
                string requestUri = "https://xxx.dfs.core.windows.net/t11/b.txt?action=append&position=0";
                var method = new HttpMethod("PATCH");
    
                // read local file as stream
                var mystream = File.OpenRead(@"D:\temp\1\test1.txt");
                Console.WriteLine($"the stream length is: {mystream.Length}");
                Console.WriteLine($"the position of the stream is: {mystream.Position}");
    
                var stream_length = mystream.Length;
    
                var request = new HttpRequestMessage(method, requestUri)
                {
                    //Content = new StringContent(upload_string)
                    Content = new StreamContent(mystream)
                };
    
                // Add some defined headers
                request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
                request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain"));
                var i = request.Content.AsString().Length;
                Console.WriteLine(request.Content.AsString());
    
                var httpClient = new HttpClient();
                var result = httpClient.SendAsync(request).Result;
    
                Console.WriteLine("append result status code: "+ (int)result.StatusCode);
    
                //for flush    
                string requestUri_2 = "https://xxx.dfs.core.windows.net/t11/b.txt?action=flush&position="+stream_length;
    
                var request_2 = new HttpRequestMessage(method,requestUri_2);
    
                using (HttpClient httpClient_2 = new HttpClient())
                {
                    httpClient_2.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
                    HttpResponseMessage response = httpClient_2.SendAsync(request_2).Result;
                    Console.WriteLine("flush result status code: " + (int)response.StatusCode);
                }