使用rest API从azure blob容器中获取所有文件,我已经尝试过此代码,但我面临403错误

时间:2017-10-25 09:52:55

标签: azure azure-storage azure-blob-storage

我已经使用RestAPI获取容器了 使用RestAPI从blob存储容器中获取所有文件。

我已经使用了这个API。

   private const string ListofFilesURL = "https://{0}.blob.core.windows.net/{1}?restype=container&comp=list&maxresults=10";

我的代码如下,

 public async void ListofFilessinBlob(string containername)
     {

            string Requesturl = string.Format(ListofFilesURL, storageAccount, containername);
            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", AuthorizationHeader2("GET", now, request, storageAccount, storageKey, containername));
            // var response =  request.GetResponseAsync().Result;
            await request.GetResponseAsync();
            using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
            {
                resp.StatusCode.ToString();
                WebHeaderCollection header = resp.Headers;

                var encoding = ASCIIEncoding.ASCII;
                using (var reader = new System.IO.StreamReader(resp.GetResponseStream(), encoding))
                {
                    string responseText = reader.ReadToEnd();

                }
            }

        }

我的授权标题方法如下,

private string AuthorizationHeader2(string method,string now,HttpWebRequest request,string storageAccount,string storageKey,string containerName)     {

    string headerResource = $"x-ms-date:{now}\nx-ms-version:2015-12-11";
    string canonicalizedResource = $"/{storageAccount}/{containerName}\ncomp:list\nmaxresults:10";
    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;
}

enter image description here

您可以看到此错误。我已经尝试过这段代码。

1 个答案:

答案 0 :(得分:2)

您的canonicalizedResource字符串不包含所有查询字符串参数。它应该是:

string canonicalizedResource = $"/{storageAccount}/{containerName}\ncomp:list\nmaxresults:10\nrestype:container";

从文档链接:

enter image description here