使用rest api的azure数据湖存储中的更新文件出现问题

时间:2019-07-18 07:18:08

标签: azure rest azure-data-lake

我正在尝试从本地文件中获取数据,并将数据添加到其中一个文件中,并将其添加到adls(azure数据湖存储)中,我遵循以下URL。 click here

我如何使用c#将参数和主体传递给api,因为这么多标头没有任何值

在我尝试执行的代码下面,但没有成功。

HttpClient httpClient = new HttpClient();
        {
            String storageAccountUrl = "https://XXXXXXXXXX.dfs.core.windows.net/XXXXX/test.txt?action=flush&position=0&retainUncommittedData=false&close=true";
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authenticationToken);
            var httpResponseMessage = await httpClient.PutAsync(storageAccountUrl, new StringContent("test"));
            string resp = await httpResponseMessage.Content.ReadAsStringAsync();

        }

我应该如何传递其他标头,这是传递正文的正确方法。

2 个答案:

答案 0 :(得分:1)

根据文档,某些标题只是可选的,这意味着您无需在标题中进行设置。您只需要设置所需的标题。

更新

有关如何使用Httpclient在C#中发送修补程序请求,您可能会在Internet上获得许多示例和文档。这只是一个示例:

static void Main(string[] args)
{
    string requestUri = "the uri, https://.....";
    var method = new HttpMethod("PATCH");

    var request = new HttpRequestMessage(method, requestUri)
    {
        Content = new StringContent("Test string")
    };

    // Add some defined headers
    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "Your Oauth token");
    request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    // Add some other headers or custom headers
    request.Headers.TryAddWithoutValidation("custome_header", "value");

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

    Console.WriteLine(result);

    Console.ReadLine();
}

答案 1 :(得分:1)

如果更新一个空文件,下面的代码可以正常工作:

        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();
        }