Skip to content

Instantly share code, notes, and snippets.

@nmackenzie
Created September 8, 2013 06:47
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 nmackenzie/6482489 to your computer and use it in GitHub Desktop.
Save nmackenzie/6482489 to your computer and use it in GitHub Desktop.
public void LeaseBlob(String containerName, String blobName)
{
String requestMethod = "PUT";
String urlPath = String.Format("{0}/{1}?comp=lease", containerName, blobName);
String modifiedUrlPath = String.Format("{0}/{1}\ncomp:lease", containerName, blobName);
const Int32 contentLength = 0;
String storageServiceVersion = "2012-02-12";
String dateInRfc1123Format = DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture);
String leaseAction = "acquire";
String leaseDuration = "60";
String canonicalizedHeaders = String.Format(
"x-ms-date:{0}\nx-ms-lease-action:{1}\nx-ms-lease-duration:{2}\nx-ms-version:{3}",
dateInRfc1123Format,
leaseAction,
leaseDuration,
storageServiceVersion);
String canonicalizedResource = String.Format("/{0}/{1}", AzureStorageConstants.Account, modifiedUrlPath);
String stringToSign = String.Format(
"{0}\n\n\n{1}\n\n\n\n\n\n\n\n\n{2}\n{3}",
requestMethod,
contentLength,
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-date", dateInRfc1123Format);
request.Headers.Add("x-ms-lease-action", leaseAction);
request.Headers.Add("x-ms-lease-duration", leaseDuration);
request.Headers.Add("x-ms-version", storageServiceVersion);
request.Headers.Add("Authorization", authorizationHeader);
request.ContentLength = contentLength;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
String leaseId = response.Headers["x-ms-lease-id"];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment