0

I'm using ADLS Gen2 using rest api Path-Update i'm trying to update data into already created created blank file into ADLS. but whenever i'm trying to use the API i'm getting below response from API. {StatusCode: 202, ReasonPhrase: 'Accepted'} but still the file will be empty.

string requestUri = "https://XXXXXXX.dfs.core.windows.net/XXXXX/abc.txt?action=append&position=0";// &retainUncommittedData=false&close=true";
            dynamic method = new HttpMethod("PATCH");
            dynamic request = new HttpRequestMessage(method, requestUri)
            {
                Content = new StringContent("\"requestBody\":\"test\"")
            };`enter code here`
            // Add some defined headers
            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authenticationToken);
            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/octet-stream"));

            // Add some other headers or custom headers
            request.Headers.TryAddWithoutValidation("Content-Length", "0");

            dynamic httpClient = new HttpClient();
            dynamic result = httpClient.SendAsync(request).Result;

i expect the data should be updated in file but now i'm getting 202 Accepted as a response code but file is not updated with data

also i tried append with flush operation below is the code i'm getting 405 error

string requestUri = "https://XXXXXX.dfs.core.windows.net/XXXXX/abc.txt?action=append&position=0";// &retainUncommittedData=false&close=true";
        var method = new HttpMethod("PATCH");

        var request = new HttpRequestMessage(method, requestUri)
        {
            Content = new StringContent("\"requestBody\":\"test\"")
        };

        // Add some defined headers
        request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authenticationToken);
        request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain"));

        // Add some other headers or custom headers
        // request.Headers.TryAddWithoutValidation("Content-Length", "0");

        var httpClient = new HttpClient();
        var result = httpClient.SendAsync(request).Result;

        string requestUri1 = "https://XXXXX.dfs.core.windows.net/XXXXXX/abc.txt?action=flush&position=0";//&retainUncommittedData=false&close=true";
        using (HttpClient httpClient1 = new HttpClient())
        {

            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authenticationToken);
            HttpResponseMessage response = (httpClient.PutAsync(requestUri1, null)).Result;


        }
dev
  • 163
  • 1
  • 11

1 Answers1

2

Update:

If the files in adls gen2 is empty, you can use the method below:

        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/c.txt?action=append&position=0";
            var method = new HttpMethod("PATCH");

            string upload_string = "have a nice day!";

            var request = new HttpRequestMessage(method, requestUri)
            {
                Content = new StringContent(upload_string)
            };

            // 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/c.txt?action=flush&position="+upload_string.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);
            }

          Console.ReadLine();
        }

Test result as below, and I also check in azure, the data is flushed into the file.

enter image description here


Once you have received the 202 Accepted you can then call the action=flush and pass the position that you want the data to be flushed to. like below:

https://$STORAGE_ACCOUNT_NAME.dfs.core.windows.net/mydata/data/file1?action=flush&position=10
Ivan Glasenberg
  • 29,865
  • 2
  • 44
  • 60