1
votes

Hi Im wondering if the azure blob service api http://msdn.microsoft.com/en-us/library/dd135733.aspx

can be called using c#. Id like to upload a file e.g a word document to a storage location, the http method is "put" and the rest url is

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

would this code work?

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

1 Answers

1
votes

No, this wouldn't work. Authentication to Windows Azure storage involves signing the headers of the request. (See http://msdn.microsoft.com/en-us/library/dd179428.aspx.)

Note that the .NET StorageClient library that ships with the SDK is redistributable, so you can just add a reference to that and do (from memory):

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