私はAzureの残りのAPIとブロブを入れようとしています。私は "GET"リクエストを成功させましたが、 "PUT"リクエストに問題がありました。私は "PUT"リクエストを作成しようとすると、私は404error(私はstackoverflowで同じ投稿を見たことがあるが、それは私を助けてくれませんでした).i私が使用しているMessageSignatureが正しい(私はMessageSignaturePutを試みたが、didntの仕事)。助言がありますか?灰色の残りのAPIはブロブを入れます
public void UploadBlobWithRestAPI(string uri, DateTime now)
{
string blobName = "test.txt";
string method = "PUT";
string sampleContent = "This is sample text.";
int contentLength = Encoding.UTF8.GetByteCount(sampleContent);
string queryString = (new Uri(uri)).Query;
string blobContainerUri = uri.Substring(0, uri.Length - queryString.Length);
string requestUri = string.Format(CultureInfo.InvariantCulture, "{0}/{1}{2}", blobContainerUri, blobName, queryString);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
string nnow = now.ToString("R", System.Globalization.CultureInfo.InvariantCulture);
request.Method = method;
request.Headers.Add("x-ms-version", "2015-02-21");
request.Headers.Add("x-ms-date", nnow);
request.ContentType = "text/plain; charset=UTF-8";
request.Headers.Add("x-ms-blob-type", "BlockBlob");
request.ContentLength = contentLength;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(Encoding.UTF8.GetBytes(sampleContent), 0, contentLength);
}
request.Headers.Add("Authorization", AuthorizationHeader(method, now, request, "", ""));
using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
{
MessageBox.Show(resp.StatusCode.ToString());
}
}
public string AuthorizationHeader(string method, DateTime now, HttpWebRequest request,
string ifMatch = "", string md5 = "")
{
string MessageSignature;
string StorageKey = "xxx";
string StorageAccount = "upgradedevstorage";
MessageSignature = String.Format("{0}\n\n\n{1}\n{5}\n\n\n\n{2}\n\n\n\n{3}{4}",
method,
(method == "GET" || method == "HEAD") ? String.Empty : request.ContentLength.ToString(),
ifMatch,
GetCanonicalizedHeaders(request),
GetCanonicalizedResource(request.RequestUri, StorageAccount),
md5
);
??? //string MessageSignaturePut= String.Format("{0}\n\n{1}\n\n{2}{3}",
// method,
// "text/plain; charset=UTF-8",
// GetCanonicalizedHeaders(request),
// GetCanonicalizedResource(request.RequestUri, StorageAccount)
// );
byte[] SignatureBytes = System.Text.Encoding.UTF8.GetBytes(MessageSignature);
System.Security.Cryptography.HMACSHA256 SHA256 =
new System.Security.Cryptography.HMACSHA256(Convert.FromBase64String(StorageKey));
string signature = Convert.ToBase64String(SHA256.ComputeHash(SignatureBytes));
string AuthorizationHeader = "SharedKey " + StorageAccount
+ ":" + Convert.ToBase64String(SHA256.ComputeHash(SignatureBytes));
return AuthorizationHeader;
}
。 Shared Access Signatureの作成方法を教えてください。私はSASに含まれる権限について知りたいと思っています。 –