使用adls gen 2 rest api无法将数据上传到blob存储 [英] Failing to upload data to blob storage using adls gen 2 rest api

查看:64
本文介绍了使用adls gen 2 rest api无法将数据上传到blob存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用adls gen2 rest api将简单字符串上传到blob文件。我收到403禁止错误返回说我的授权标题不正确。我上传的数据只是一个简单的字符串,我检查了我的共享
键是否正确。我也已经使用api创建了文件。

I am trying to upload a simple string to a blob file using the adls gen2 rest api. I am getting a 403 forbidden error returned saying my authorization header is incorrect. The data i am uploading is just a simple string and I have checked that my shared key is correct. I have also already created the file using the api.

编码前的授权标头签名:

Authorization header signature before encoding:

我的代码如下所示:

private static async Task pathUpdate(string storageAccountName, string storageAccountKey, string filesystem, string path, string action, string postData, CancellationToken cancellationToken)
        {

            // Construct the URI. This will look like this:
            //   https://myaccount.dfs.core.windows.net/resource
            String uri = string.Format("https://{0}.dfs.core.windows.net/{1}/{2}?action={3}?position=0", storageAccountName, filesystem, path, action);
            Console.WriteLine(uri);
            // Set this to whatever payload you desire. Ours is null because 
            //   we're not passing anything in.
            
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] byte1 = encoding.GetBytes(postData);
            Byte[] requestPayload = byte1;
            //Instantiate the request message with a null payload.
            var method = new HttpMethod("PATCH");
            using (var httpRequestMessage = new HttpRequestMessage(method, uri)
            { Content = (requestPayload == null) ? null : new ByteArrayContent(requestPayload) })
            {

                // Add the request headers for x-ms-date and x-ms-version.
                DateTime now = DateTime.UtcNow;
                httpRequestMessage.Headers.Add("x-ms-date", now.ToString("R", CultureInfo.InvariantCulture));
                httpRequestMessage.Headers.Add("x-ms-version", "2018-11-09");
                httpRequestMessage.Headers.Add("ContentLength", byte1.Length.ToString());
                //httpRequestMessage.Headers.Add("ContentType", "application/octet-stream");
                //httpRequestMessage.Headers.A
                // If you need any additional headers, add them here before creating
                //   the authorization header. 

                // Add the authorization header.
                httpRequestMessage.Headers.Authorization = AzureStorageAuthenticationHelper.GetAuthorizationHeader(
                   storageAccountName, storageAccountKey, now, httpRequestMessage);

                // Send the request.
                using (HttpResponseMessage httpResponseMessage = await new HttpClient().SendAsync(httpRequestMessage, cancellationToken))
                {
                    Console.WriteLine(httpResponseMessage);
                    // If successful (status code = 200), 
                    //   parse the XML response for the container names.
                    if (httpResponseMessage.StatusCode == HttpStatusCode.OK)
                    {
                        String xmlString = await httpResponseMessage.Content.ReadAsStringAsync();
                        Console.WriteLine(xmlString);

                    }
                }
            }
        }

和授权标题构造:

namespace StorageRestApiAuth
{
    internal static class AzureStorageAuthenticationHelper
    {
        internal static AuthenticationHeaderValue GetAuthorizationHeader(
           string storageAccountName, string storageAccountKey, DateTime now,
           HttpRequestMessage httpRequestMessage, string ifMatch = "", string md5 = "")
        {
            // This is the raw representation of the message signature.
            HttpMethod method = httpRequestMessage.Method;
            String MessageSignature = String.Format("{0}\n\n\n{1}\n{5}\n\n\n\n{2}\n\n\n\n{3}{4}",
                      method.ToString(),
                      (method == HttpMethod.Get || method == HttpMethod.Put) ? String.Empty
                        : httpRequestMessage.Content.Headers.ContentLength.ToString(),
                      ifMatch,
                      GetCanonicalizedHeaders(httpRequestMessage),
                      GetCanonicalizedResource(httpRequestMessage.RequestUri, storageAccountName),
                      md5);

            // Now turn it into a byte array.
            byte[] SignatureBytes = Encoding.UTF8.GetBytes(MessageSignature);

            // Create the HMACSHA256 version of the storage key.
            HMACSHA256 SHA256 = new HMACSHA256(Convert.FromBase64String(storageAccountKey));

            // Compute the hash of the SignatureBytes and convert it to a base64 string.
            string signature = Convert.ToBase64String(SHA256.ComputeHash(SignatureBytes));

            AuthenticationHeaderValue authHV = new AuthenticationHeaderValue("SharedKey",
                storageAccountName + ":" + Convert.ToBase64String(SHA256.ComputeHash(SignatureBytes)));
            return authHV;
        }

        private static string GetCanonicalizedHeaders(HttpRequestMessage httpRequestMessage)
        {
            var headers = from kvp in httpRequestMessage.Headers
                          where kvp.Key.StartsWith("x-ms-", StringComparison.OrdinalIgnoreCase)
                          orderby kvp.Key
                          select new { Key = kvp.Key.ToLowerInvariant(), kvp.Value };

            StringBuilder sb = new StringBuilder();

            // Create the string in the right format; this is what makes the headers "canonicalized" --
            //   it means put in a standard format. http://en.wikipedia.org/wiki/Canonicalization
            foreach (var kvp in headers)
            {
                StringBuilder headerBuilder = new StringBuilder(kvp.Key);
                char separator = ':';

                // Get the value for each header, strip out \r\n if found, then append it with the key.
                foreach (string headerValues in kvp.Value)
                {
                    string trimmedValue = headerValues.TrimStart().Replace("\r\n", String.Empty);
                    headerBuilder.Append(separator).Append(trimmedValue);

                    // Set this to a comma; this will only be used 
                    //   if there are multiple values for one of the headers.
                    separator = ',';
                }
                sb.Append(headerBuilder.ToString()).Append("\n");
            }
            return sb.ToString();
        }      

        private static string GetCanonicalizedResource(Uri address, string storageAccountName)
        {
            // The absolute path is "/" because for we're getting a list of containers.
            StringBuilder sb = new StringBuilder("/").Append(storageAccountName).Append(address.AbsolutePath);

            // Address.Query is the resource, such as "?comp=list".
            // This ends up with a NameValueCollection with 1 entry having key=comp, value=list.
            // It will have more entries if you have more query parameters.
            NameValueCollection values = HttpUtility.ParseQueryString(address.Query);

            foreach (var item in values.AllKeys.OrderBy(k => k))
            {
                sb.Append('\n').Append(item).Append(':').Append(values[item]);
            }

            return sb.ToString();

        }
    }
}

感谢您的帮助!

推荐答案

你好Ewan,

Hi Ewan,

我已经发布了一个步骤我的回答中的步骤指南
此主题
。请看看我的答案,看看它是否对您有所帮助,否则我们很乐意继续深入研究。

I have posted a step by step guide in my answer on this thread. Please have a look at my answer and see if it helps you, else we can gladly continue to dive deeper. 


这篇关于使用adls gen 2 rest api无法将数据上传到blob存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆