【问题标题】:Azure BLOB storage REST call from C#来自 C# 的 Azure BLOB 存储 REST 调用
【发布时间】:2018-06-20 12:58:44
【问题描述】:

我正在尝试通过控制台应用程序与 Azure Blob 存储上的容器进行通信。我不能使用 SDK,因此 REST 是我唯一的选择。语言是 C# 和 .NET 4.5.2。

我试过这两个代码,都返回相同的错误

Azure rest API put blob On StackOverflow

Azure Blob Storage Part 5(Non-StackOverflow)

我收到的错误是 400 Bad Request

有其他人遇到过同样的问题并成功解决了吗?

我已经为几乎所有内容添加了带有 (*) 的 CORS 规则

代码与两个链接完全相同,因此我不会在此处添加。

    class Program
   {
    static void Main(string[] args)
    {

        UploadBlobWithRestAPI();
    }

    public static void UploadBlobWithRestAPI()
    {

        string storageKey = "ffFJwPXTqyYvRoubNQEti/aQUUMwn41BG3KDtl/yGpG4DR1eKaHRq6Bhbw==";
        string storageAccount = "xyz";
        string containerName = "notes";
        string blobName = "test567";

        string method = "PUT";
        string sampleContent = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla id euismod urna. Maecenas scelerisque dolor purus, sed ullamcorper ipsum lobortis non. Nulla est justo, sodales at venenatis a, faucibus";
        int contentLength = Encoding.UTF8.GetByteCount(sampleContent);

        string requestUri = $"https://xyz.blob.core.windows.net/notes/test567";

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);

        string now = DateTime.UtcNow.ToString("R");

        request.Method = method;
        request.ContentType = "text/plain; charset=UTF-8";
        request.ContentLength = contentLength;

        request.Headers.Add("x-ms-version", "2018-01-11");
        request.Headers.Add("x-ms-date", now);
        request.Headers.Add("x-ms-blob-type", "BlockBlob");
        request.Headers.Add("Authorization", AuthorizationHeader2(method, now, request, storageAccount, storageKey, containerName, blobName));

        using (Stream requestStream = request.GetRequestStream())
        {
            requestStream.Write(Encoding.UTF8.GetBytes(sampleContent), 0, contentLength);
        }

        using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
        {
            Console.WriteLine(resp.StatusCode.ToString());
            Console.ReadKey();
        }

    }

    public static string AuthorizationHeader2(string method, string now, HttpWebRequest request, string storageAccount, 
        string storageKey, string containerName, string blobName)
    {

        string headerResource = $"x-ms-blob-type:BlockBlob\nx-ms-date:"+ DateTime.UtcNow.ToString("R") +"\nx-ms-version:2018-01-11";
        string urlResource = "/xyz/notes/test567";
        string stringToSign =  method + "\n\n\n" + request.ContentLength + 
            "\n\n" + request.ContentType +"\n\n\n\n\n\n\n" + headerResource + "\n" + urlResource;


        HMACSHA256 hmac = new HMACSHA256(Convert.FromBase64String(storageKey));
        string signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));

        String AuthorizationHeader = String.Format("{0} {1}:{2}", "SharedKey", storageAccount, signature);
        return AuthorizationHeader;
    }


}

【问题讨论】:

  • 阅读How to Ask并显示相关代码。
  • 您能否编辑您的问题并包含您的代码。
  • 否,因为在使用 (HttpWebResponse resp = (HttpWebResponse)request.GetResponse()) 初始化响应对象之前它会抛出异常
  • 请添加request.Headers.Add("x-ms-blob-type", "BlockBlob"); 看看是否能解决问题。您的请求中缺少此必需的标头。

标签: c# azure azure-storage


【解决方案1】:

您的代码有两个问题:

  1. 您使用的服务版本无效。最新的存储服务 REST API 版本是 2017-04-17 而不是 2018-01-11。更改后,您将不会收到 400 错误(但您会收到 403 错误)。
  2. 在您的headerResource 中,您正在生成一个新的日期/时间值,该值与x-ms-date 标头中的日期/时间值不同。因此,您将收到 403 错误。所以基本上你的代码是:

        string headerResource = $"x-ms-blob-type:BlockBlob\nx-ms-date:" + now + "\nx-ms-version:2017-04-17";
    

我做了这两个修复,之后我就可以上传数据了。

这是完整的代码:

    public static void UploadBlobWithRestAPI()
    {

        string storageKey = "ffFJwPXTqyYvRoubNQEti/aQUUMwn41BG3KDtl/yGpG4DR1eKaHRq6Bhbw==";
        string storageAccount = "xyz";
        string containerName = "notes";
        string blobName = "test567";

        string method = "PUT";
        string sampleContent = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla id euismod urna. Maecenas scelerisque dolor purus, sed ullamcorper ipsum lobortis non. Nulla est justo, sodales at venenatis a, faucibus";
        int contentLength = Encoding.UTF8.GetByteCount(sampleContent);

        string requestUri = $"https://xyz.blob.core.windows.net/notes/test567";

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);

        string now = DateTime.UtcNow.ToString("R");

        request.Method = method;
        request.ContentType = "text/plain; charset=UTF-8";
        request.ContentLength = contentLength;

        request.Headers.Add("x-ms-version", "2017-04-17");
        request.Headers.Add("x-ms-date", now);
        request.Headers.Add("x-ms-blob-type", "BlockBlob");
        request.Headers.Add("Authorization", AuthorizationHeader2(method, now, request, storageAccount, storageKey, containerName, blobName));

        using (Stream requestStream = request.GetRequestStream())
        {
            requestStream.Write(Encoding.UTF8.GetBytes(sampleContent), 0, contentLength);
        }

        using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
        {
            Console.WriteLine(resp.StatusCode.ToString());
            Console.ReadKey();
        }

    }

    public static string AuthorizationHeader2(string method, string now, HttpWebRequest request, string storageAccount,
        string storageKey, string containerName, string blobName)
    {

        string headerResource = $"x-ms-blob-type:BlockBlob\nx-ms-date:" + now + "\nx-ms-version:2017-04-17";
        string urlResource = "/xyz/notes/test567";
        string stringToSign = method + "\n\n\n" + request.ContentLength +
            "\n\n" + request.ContentType + "\n\n\n\n\n\n\n" + headerResource + "\n" + urlResource;


        HMACSHA256 hmac = new HMACSHA256(Convert.FromBase64String(storageKey));
        string signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));

        String AuthorizationHeader = String.Format("{0} {1}:{2}", "SharedKey", storageAccount, signature);
        return AuthorizationHeader;
    }

【讨论】:

    猜你喜欢
    • 2019-03-15
    • 1970-01-01
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-13
    • 2020-12-03
    • 2021-11-02
    • 1970-01-01
    相关资源
    最近更新 更多