首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Azure Storage (Put Blob )

Azure Storage (Put Blob )
EN

Stack Overflow用户
提问于 2021-04-13 10:52:09
回答 1查看 926关注 0票数 1

我正在尝试使用Azure rest来放置一个blob。我成功地提出了一个"GET“请求,但是我对"PUT”请求有异议。当我试图发出"PUT“请求时,我会得到一个403错误(服务器无法验证请求)。确保授权头的值是正确的,包括签名。)。我在堆叠堆里看到过同样的帖子,但对我没什么帮助。有什么建议吗?

代码语言:javascript
运行
复制
 string uri = string.Format("https://{0}.blob.core.windows.net/{1}/LibraryForm.png",                storageAccountName,containerName);
        Byte[] requestPayload =  File.ReadAllBytes(imagepath);
        using (var httpRequestMessage = new HttpRequestMessage(HttpMethod.Put, 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", "2020-06-12");
            httpRequestMessage.Headers.Add("x-ms-blob-type", "BlockBlob");
            httpRequestMessage.Headers.Add("x-ms-blob-content-type", "image/png");

            // Add the authorization header.
            httpRequestMessage.Headers.Authorization = GetAuthorizationHeader(
               storageAccountName, storageAccountKey, now, httpRequestMessage);
            // Send the request.
            using (HttpResponseMessage httpResponseMessage = await new HttpClient().SendAsync(httpRequestMessage))
            {
                if (httpResponseMessage.StatusCode == HttpStatusCode.OK)
                {
                    var str = httpResponseMessage.Content.ReadAsStringAsync().Result;
                    return str;
                }
            }
        }


      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.Head) ? 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));

        // This is the actual header that will be added to the list of request headers.
        // You can stop the code here and look at the value of 'authHV' before it is returned.
        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)
    {
        StringBuilder sb = new StringBuilder("/").Append(storageAccountName).Append(address.AbsolutePath);

        // 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().ToLower();

    }
EN

回答 1

Stack Overflow用户

发布于 2021-04-14 02:51:52

如果您想用rest将文件上载到Azure Blob,请参阅fooliwng代码。

我定义了一个类来获取ShareKey

代码语言:javascript
运行
复制
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.Head) ? 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));

            // This is the actual header that will be added to the list of request headers.
            // You can stop the code here and look at the value of 'authHV' before it is returned.
            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().ToLower();

        }
    }
  1. 乌普劳德
代码语言:javascript
运行
复制
                FileInfo fileInfo = new FileInfo("D:\\sampleData\\readsample.jpg");
                string blobName = fileInfo.Name;
                string contentType = MimeMapping.GetMimeMapping(blobName);
                DateTime now = DateTime.UtcNow;
                string blobURI = string.Format("https://{0}.blob.core.windows.net/{1}/{2}", StorageAccountName, "test", blobName);
                using (var httpRequestMessage = new HttpRequestMessage(HttpMethod.Put, blobURI))
                {
                    httpRequestMessage.Headers.Add("x-ms-date", now.ToString("R", CultureInfo.InvariantCulture));
                    httpRequestMessage.Headers.Add("x-ms-version", "2020-06-12");
                    httpRequestMessage.Headers.Add("x-ms-blob-type", "BlockBlob");

                    httpRequestMessage.Headers.Add("x-ms-blob-content-type", contentType);
                    httpRequestMessage.Content = new StreamContent(fileInfo.OpenRead());
                    httpRequestMessage.Headers.Authorization = AzureStorageAuthenticationHelper.GetAuthorizationHeader(
                StorageAccountName, StorageAccountKey, now, httpRequestMessage);

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

                        }
                    }
                }

此外,使用Azure实现上传过程也是一种简单的方法。关于如何使用azure,请参考这里

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67073448

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档