您需要x-ms-blob-type
为 blob 类型定义请求标头 ( ) 并将其值设置为BlockBlob
. 同样对于Put
请求,您还需要定义Content-Length
请求标头。我写了一篇关于共享访问签名的博客文章并使用它(使用 REST API 和存储客户端库)执行一些 blob 操作,您可以在此处阅读:http: //gauravmantri.com/2013/02/13/revisiting-windows-天蓝色共享访问签名/。
这是上传 blob 的帖子中的代码。它使用 HttpWebRequest/HttpWebResponse 而不是 WebClient:
static void UploadBlobWithRestAPISasPermissionOnBlobContainer(string blobContainerSasUri)
{
string blobName = "sample.txt";
string sampleContent = "This is sample text.";
int contentLength = Encoding.UTF8.GetByteCount(sampleContent);
string queryString = (new Uri(blobContainerSasUri)).Query;
string blobContainerUri = blobContainerSasUri.Substring(0, blobContainerSasUri.Length - queryString.Length);
string requestUri = string.Format(CultureInfo.InvariantCulture, "{0}/{1}{2}", blobContainerUri, blobName, queryString);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
request.Method = "PUT";
request.Headers.Add("x-ms-blob-type", "BlockBlob");
request.ContentLength = contentLength;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(Encoding.UTF8.GetBytes(sampleContent), 0, contentLength);
}
using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
{
}
}