Azure Blob获取存储使用面向403禁止错误的http请求获取所有Blob

时间:2017-10-25 04:49:16

标签: azure azure-storage azure-storage-blobs

我想使用http客户端请求从azure存储中获取所有blob容器。 我做了一个样本,但我面临禁止403错误。

我附上了我的代码,

        private const string ListofContainersURL = "https://{0}.blob.core.windows.net/?comp=list&maxresults=3"; //https://myaccount.blob.core.windows.net/?comp=list&maxresults=3


       public  string ListofContainersinBlob()
            {

                string Requesturl = string.Format(ListofContainersURL, storageAccount );
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Requesturl);
                string now = DateTime.UtcNow.ToString("R");
                request.Method = "GET";
                request.Headers.Add("x-ms-version", "2015-12-11");
                request.Headers.Add("x-ms-date", now);
                request.Headers.Add("Authorization", AuthorizationHeader1("GET", now, request, storageAccount, storageKey));
                var response =  request.GetResponseAsync();

                using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
                {
                    return resp.StatusCode.ToString();
                }

            }


            private string AuthorizationHeader1(string method, string now, HttpWebRequest request, string storageAccount, string storageKey)
            {
               string headerResource = $"x-ms-blob-type:BlockBlob\nx-ms-date:{now}\nx-ms-version:2015-12-11";
                string urlResource = $"/{storageAccount}";
                String AuthorizationHeader = String.Format("{0} {1}:{2}", "SharedKey", storageAccount, storageKey);
                return AuthorizationHeader;
            }

1 个答案:

答案 0 :(得分:2)

您的AuthorizationHeader1方法存在一些问题。有关如何构建授权标头的说明,请参阅Authentication for the Azure Storage Services。请尝试以下代码:

    private static string AuthorizationHeader1(string method, string now, HttpWebRequest request, string storageAccount, string storageKey)
    {
        string headerResource = $"x-ms-date:{now}\nx-ms-version:2015-12-11";
        string canonicalizedResource = $"/{storageAccount}/\ncomp:list\nmaxresults:3";
        var contentEncoding = "";
        var contentLanguage = "";
        var contentLength = "";
        var contentMd5 = "";
        var contentType = "";
        var date = "";
        var ifModifiedSince = "";
        var ifMatch = "";
        var ifNoneMatch = "";
        var ifUnmodifiedSince = "";
        var range = "";
        var stringToSign = $"{method}\n{contentEncoding}\n{contentLanguage}\n{contentLength}\n{contentMd5}\n{contentType}\n{date}\n{ifModifiedSince}\n{ifMatch}\n{ifNoneMatch}\n{ifUnmodifiedSince}\n{range}\n{headerResource}\n{canonicalizedResource}";
        var signature = "";
        using (var hmacSha256 = new HMACSHA256(Convert.FromBase64String(storageKey)))
        {
            var dataToHmac = Encoding.UTF8.GetBytes(stringToSign);
            signature = Convert.ToBase64String(hmacSha256.ComputeHash(dataToHmac));
        }
        String AuthorizationHeader = String.Format("{0} {1}:{2}", "SharedKey", storageAccount, signature);
        return AuthorizationHeader;
    }