Created
September 8, 2013 06:47
-
-
Save nmackenzie/6482484 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void PutBlob(String containerName, String blobName) | |
{ | |
String requestMethod = "PUT"; | |
String urlPath = String.Format("{0}/{1}", containerName, blobName); | |
String storageServiceVersion = "2012-02-12"; | |
String dateInRfc1123Format = DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture); | |
String content = "Andrew Carnegie was born in Dunfermline"; | |
UTF8Encoding utf8Encoding = new UTF8Encoding(); | |
Byte[] blobContent = utf8Encoding.GetBytes(content); | |
Int32 blobLength = blobContent.Length; | |
const String blobType = "BlockBlob"; | |
String canonicalizedHeaders = String.Format( | |
"x-ms-blob-type:{0}\nx-ms-date:{1}\nx-ms-version:{2}", | |
blobType, | |
dateInRfc1123Format, | |
storageServiceVersion); | |
String canonicalizedResource = String.Format("/{0}/{1}", AzureStorageConstants.Account, urlPath); | |
String stringToSign = String.Format( | |
"{0}\n\n\n{1}\n\n\n\n\n\n\n\n\n{2}\n{3}", | |
requestMethod, | |
blobLength, | |
canonicalizedHeaders, | |
canonicalizedResource); | |
String authorizationHeader = Utility.CreateAuthorizationHeader(stringToSign); | |
Uri uri = new Uri(AzureStorageConstants.BlobEndPoint + urlPath); | |
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); | |
request.Method = requestMethod; | |
request.Headers.Add("x-ms-blob-type", blobType); | |
request.Headers.Add("x-ms-date", dateInRfc1123Format); | |
request.Headers.Add("x-ms-version", storageServiceVersion); | |
request.Headers.Add("Authorization", authorizationHeader); | |
request.ContentLength = blobLength; | |
using (Stream requestStream = request.GetRequestStream()) | |
{ | |
requestStream.Write(blobContent, 0, blobLength); | |
} | |
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) | |
{ | |
String ETag = response.Headers["ETag"]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment