Skip to content

Instantly share code, notes, and snippets.

@azchohfi
Created May 18, 2013 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azchohfi/5605921 to your computer and use it in GitHub Desktop.
Save azchohfi/5605921 to your computer and use it in GitHub Desktop.
class AzureStorageConstants
{
public static readonly string KeyString = "";
public static readonly string KeySecondString = "";
public static readonly string Account = "";
public static readonly string BlobEndPoint = "http://***.blob.core.windows.net/";
public static readonly string SharedKeyAuthorizationScheme = "";
public static readonly byte[] Key = Convert.FromBase64String(AzureStorageConstants.KeySecondString);
}
public void PutBlob(String containerName, String blobName, byte[] blobContent, EventHandler<DownloadProgressEventArgs> UploadStatusHandler)
{
String requestMethod = "PUT";
String urlPath = String.Format("{0}/{1}", containerName, blobName);
String storageServiceVersion = "2009-09-19";
String dateInRfc1123Format = DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture);
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 = CreateAuthorizationHeader(stringToSign);
Uri uri = new Uri(AzureStorageConstants.BlobEndPoint + urlPath);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = requestMethod;
request.Headers["x-ms-blob-type"] = blobType;
request.Headers["x-ms-date"] = dateInRfc1123Format;
request.Headers["x-ms-version"] = storageServiceVersion;
request.Headers["Authorization"] = authorizationHeader;
request.ContentLength = blobLength;
request.BeginGetRequestStream((result) =>
{
using (Stream requestStream = request.EndGetRequestStream(result))
{
// Read chunks of this file
byte[] buffer = new Byte[1024 * 32];
int bytesRead = 0;
MemoryStream stream = new MemoryStream(blobContent.Length);
stream.Write(blobContent, 0, blobContent.Length);
stream.Seek(0, SeekOrigin.Begin);
int bytesUploaded = 0;
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0)
{
requestStream.Write(buffer, 0, bytesRead);
requestStream.Flush(); // Will block until data is sent
bytesUploaded += bytesRead;
UploadStatusHandler(this, new DownloadProgressEventArgs(bytesUploaded, blobLength, bytesUploaded / blobLength, null));
}
//requestStream.Write(blobContent, 0, blobLength);
}
request.BeginGetResponse((result2) =>
{
using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result2))
{
String ETag = response.Headers["ETag"];
}
}, null);
}, null);
}
private String CreateAuthorizationHeader(String canonicalizedString)
{
String signature = string.Empty;
using (HMACSHA256 hmacSha256 = new HMACSHA256(AzureStorageConstants.Key))
{
Byte[] dataToHmac = System.Text.Encoding.UTF8.GetBytes(canonicalizedString);
signature = Convert.ToBase64String(hmacSha256.ComputeHash(dataToHmac));
}
String authorizationHeader = String.Format(
CultureInfo.InvariantCulture,
"{0} {1}:{2}",
AzureStorageConstants.SharedKeyAuthorizationScheme,
AzureStorageConstants.Account,
signature);
return authorizationHeader;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment