1

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

可以使用c#调用。我想上传一个文件,例如一个word文档到一个存储位置,http方法是“put”,其余的url是

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( ));
4

1 回答 1

1

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

请注意,SDK 附带的 .NET StorageClient 库是可再分发的,因此您只需添加对它的引用并执行(从内存中):

 CloudStorageAccount.Parse("<connection string>")
    .CreateCloudBlobClient()
    .GetBlobReference("mycontainer/myblob")
    .UploadByteArray(data);
于 2010-07-28T17:18:03.397 回答