尝试在 c# 中上传到 Azure 时出现 400(未指定此请求必需的 HTTP 标头。)

Man*_*el 6 c# rest azure

我正在尝试从 C# 将简单的 emtpy .txt 上传到 Azure blob 存储(目前只是概念证明)。我使用 SAS 令牌来执行此操作(该令牌适用于迄今为止我尝试过的所有其他情况,例如列出容器或删除 blob)。但是,当尝试创建新的 blob 时,我收到错误400(未指定此请求必需的 HTTP 标头。) 我想我以错误的方式配置了请求,但我无法找出正确的方式。有人可以帮忙吗?可以在此处找到 Put Rest API 调用。

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Xml.Linq;


namespace ConsoleProgram
{
    public class Class1
    {
        private static string URL = "https://example.blob.core.windows.net/";
        private static string sasToken = "sv=2019-12-12&ss=b&srt=sco&sp=rwdlacx&se=2021-02-02T15:52:32Z&st=2021-02-02T07:52:32Z&spr=https&sig=realSig";
        private static string command =  "";
        private static string commandType = "Put";


        static void PutBlob()
        {
            URL = "https://example.blob.core.windows.net/test/hello2.txt";
            command = "?";
            commandType = "Put";

        }
        static void ListContainers()
        {
            URL = "https://example.blob.core.windows.net/";
            command = "?comp=list&";
            commandType = "Get";
        }
        static void ListBlobs()
        {
            URL = "https://example.blob.core.windows.net/test";
            command = "?restype=container&comp=list&";
            commandType = "Get";
        }
        static void DeleteBlob()
        {
            URL = "https://example.blob.core.windows.net/test/hello2.txt";
            command = "?";
            commandType = "Delete";
        }

        static void Main(string[] args)
        {
            PutBlob();
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri(URL);

            // Add an Accept header for JSON format.
            client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage response;
            // List data response.
            if (commandType == "Get")
            {
                response = client.GetAsync(command + sasToken).Result;  // Blocking call! Program will wait here until a response is received or a timeout occurs.
            }
            else if (commandType == "Delete")
            {
                response = client.DeleteAsync(command + sasToken).Result;
            }
            else
            {
                // This is the Code that causes the problems

               var s=new StringContent("hello2.txt");
                response = client.PutAsync(command+sasToken,s).Result;
            }

                if (response.IsSuccessStatusCode)
            {
                Console.WriteLine("---------------Positive Responce----------------");
                Console.WriteLine(response.ToString());
                // Parse the response body.
                var dataObjects = response.Content.ReadAsStringAsync().Result;  //Make sure to add a reference to System.Net.Http.Formatting.dll
            }
            else
            {
                Console.WriteLine("---------------Negative Responce----------------");
                Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
            }

            // Make any other calls using HttpClient here.

            // Dispose once all HttpClient calls are complete. This is not necessary if the containing object will be disposed of; for example in this case the HttpClient instance will be disposed automatically when the application terminates so the following call is superfluous.
            client.Dispose();
        }


    }
}
Run Code Online (Sandbox Code Playgroud)

Man*_*el 17

简单添加行:

 client.DefaultRequestHeaders.Add("x-ms-blob-type", "BlockBlob");
              
Run Code Online (Sandbox Code Playgroud)

为我修好了。