问题描述
我已经使用RestAPI获得了容器使用RestAPI从Blob存储容器中获取所有文件.
I already get containers using RestAPIGet all files from the blob storage container using RestAPI.
我已经使用了此API.
I already use this 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();
}
}
}
我的Authorization标头方法如下,
My Authorization header method is below,
私有字符串AuthorizationHeader2(字符串方法,现在的字符串,HttpWebRequest请求,字符串storageAccount,字符串storageKey,字符串containerName) {
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;
}
您可以看到此错误.我已经尝试过此代码.
You can see this error. I already tried this code.
推荐答案
您的canonicalizedResource
字符串未包含所有querystring参数.应该是:
Your canonicalizedResource
string does not include all the querystring parameters. It should be:
string canonicalizedResource = $"/{storageAccount}/{containerName}\ncomp:list\nmaxresults:10\nrestype:container";
从文档链接:
这篇关于使用rest API从azure blob容器中获取所有文件,我已经尝试过此代码,但是我面临403错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!