c#将http put请求发送到azure存储

时间:2010-07-28 16:37:38

标签: c# azure azure-storage

嗨,我想知道azure blob服务api http://msdn.microsoft.com/en-us/library/dd135733.aspx

可以使用c#调用

。我喜欢将文件(例如word文档)上传到存储位置,http方法为“put”,其余网址为

http://myaccount.blob.core.windows.net/mycontainer/myblob

这段代码会起作用吗?

string username = "user";
string password = "password";
string data = "path to word document;

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "PUT";
request.Credentials = new NetworkCredential(username, password);
request.ContentLength = data.Length;
request.ContentType = "text/plain";

using (StreamWriter writer = new StreamWriter(request.GetRequestStream( ))) {
  writer.WriteLine(data);
}

WebResponse response = request.GetResponse( );

using (StreamReader reader = new StreamReader(response.GetResponseStream( ))) {
  while (reader.Peek( ) != -1) {
  Console.WriteLine(reader.ReadLine( ));

1 个答案:

答案 0 :(得分:0)

不,这不行。对Windows Azure存储的身份验证涉及对请求的标头进行签名。 (见http://msdn.microsoft.com/en-us/library/dd179428.aspx。)

请注意,随SDK一起提供的.NET StorageClient库是可再发行的,因此您只需添加对该内容的引用并执行(从内存中):

 CloudStorageAccount.Parse("<connection string>")
    .CreateCloudBlobClient()
    .GetBlobReference("mycontainer/myblob")
    .UploadByteArray(data);