[英]Azure BLOB storage REST call from C#
我正在嘗試通過控制台應用程序與Azure Blob存儲上的容器通信。 我無法使用SDK,因此REST是我唯一的選擇。 使用.NET 4.5.2。的語言是C#。
我試過這兩個代碼,都返回相同的錯誤
Azure Rest API將Blob放在StackOverflow上
還有其他人遇到過同樣的問題並成功解決了嗎?
我為幾乎所有內容添加了帶有(*)的CORS規則
該代碼是兩個鏈接的完全重復,因此我不在這里添加它。
class Program
{
static void Main(string[] args)
{
UploadBlobWithRestAPI();
}
public static void UploadBlobWithRestAPI()
{
string storageKey = "ffFJwPXTqyYvRoubNQEti/aQUUMwn41BG3KDtl/yGpG4DR1eKaHRq6Bhbw==";
string storageAccount = "xyz";
string containerName = "notes";
string blobName = "test567";
string method = "PUT";
string sampleContent = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla id euismod urna. Maecenas scelerisque dolor purus, sed ullamcorper ipsum lobortis non. Nulla est justo, sodales at venenatis a, faucibus";
int contentLength = Encoding.UTF8.GetByteCount(sampleContent);
string requestUri = $"https://xyz.blob.core.windows.net/notes/test567";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
string now = DateTime.UtcNow.ToString("R");
request.Method = method;
request.ContentType = "text/plain; charset=UTF-8";
request.ContentLength = contentLength;
request.Headers.Add("x-ms-version", "2018-01-11");
request.Headers.Add("x-ms-date", now);
request.Headers.Add("x-ms-blob-type", "BlockBlob");
request.Headers.Add("Authorization", AuthorizationHeader2(method, now, request, storageAccount, storageKey, containerName, blobName));
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(Encoding.UTF8.GetBytes(sampleContent), 0, contentLength);
}
using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
{
Console.WriteLine(resp.StatusCode.ToString());
Console.ReadKey();
}
}
public static string AuthorizationHeader2(string method, string now, HttpWebRequest request, string storageAccount,
string storageKey, string containerName, string blobName)
{
string headerResource = $"x-ms-blob-type:BlockBlob\nx-ms-date:"+ DateTime.UtcNow.ToString("R") +"\nx-ms-version:2018-01-11";
string urlResource = "/xyz/notes/test567";
string stringToSign = method + "\n\n\n" + request.ContentLength +
"\n\n" + request.ContentType +"\n\n\n\n\n\n\n" + headerResource + "\n" + urlResource;
HMACSHA256 hmac = new HMACSHA256(Convert.FromBase64String(storageKey));
string signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));
String AuthorizationHeader = String.Format("{0} {1}:{2}", "SharedKey", storageAccount, signature);
return AuthorizationHeader;
}
}
您的代碼有兩個問題:
2017-04-17
而不是2018-01-11
。 更改后,將不會出現400錯誤(但會出現403錯誤)。 在headerResource
,您將生成一個新的日期/時間值,該值將不同於x-ms-date
標頭中的日期/時間值。 因此,您將得到403錯誤。 因此,基本上您的代碼將是:
string headerResource = $"x-ms-blob-type:BlockBlob\\nx-ms-date:" + now + "\\nx-ms-version:2017-04-17";
我做了這兩個修復,之后就可以上傳數據了。
這是完整的代碼:
public static void UploadBlobWithRestAPI()
{
string storageKey = "ffFJwPXTqyYvRoubNQEti/aQUUMwn41BG3KDtl/yGpG4DR1eKaHRq6Bhbw==";
string storageAccount = "xyz";
string containerName = "notes";
string blobName = "test567";
string method = "PUT";
string sampleContent = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla id euismod urna. Maecenas scelerisque dolor purus, sed ullamcorper ipsum lobortis non. Nulla est justo, sodales at venenatis a, faucibus";
int contentLength = Encoding.UTF8.GetByteCount(sampleContent);
string requestUri = $"https://xyz.blob.core.windows.net/notes/test567";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
string now = DateTime.UtcNow.ToString("R");
request.Method = method;
request.ContentType = "text/plain; charset=UTF-8";
request.ContentLength = contentLength;
request.Headers.Add("x-ms-version", "2017-04-17");
request.Headers.Add("x-ms-date", now);
request.Headers.Add("x-ms-blob-type", "BlockBlob");
request.Headers.Add("Authorization", AuthorizationHeader2(method, now, request, storageAccount, storageKey, containerName, blobName));
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(Encoding.UTF8.GetBytes(sampleContent), 0, contentLength);
}
using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
{
Console.WriteLine(resp.StatusCode.ToString());
Console.ReadKey();
}
}
public static string AuthorizationHeader2(string method, string now, HttpWebRequest request, string storageAccount,
string storageKey, string containerName, string blobName)
{
string headerResource = $"x-ms-blob-type:BlockBlob\nx-ms-date:" + now + "\nx-ms-version:2017-04-17";
string urlResource = "/xyz/notes/test567";
string stringToSign = method + "\n\n\n" + request.ContentLength +
"\n\n" + request.ContentType + "\n\n\n\n\n\n\n" + headerResource + "\n" + urlResource;
HMACSHA256 hmac = new HMACSHA256(Convert.FromBase64String(storageKey));
string signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));
String AuthorizationHeader = String.Format("{0} {1}:{2}", "SharedKey", storageAccount, signature);
return AuthorizationHeader;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.