Skip to content

Instantly share code, notes, and snippets.

@jamiekt
Created September 12, 2014 14:39
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jamiekt/2efdd38c1b26d78cc6cd to your computer and use it in GitHub Desktop.
Save jamiekt/2efdd38c1b26d78cc6cd to your computer and use it in GitHub Desktop.
Create a BLOB in Azure BLOB Storage using the REST API and Powershell
$method = "PUT"
$headerDate = '2014-02-14'
$headers = @{"x-ms-version"="$headerDate"}
$StorageAccountName = "<your account name>"
$StorageContainerName = "etl"
$StorageAccountKey = "<your account key>"
$Url = "https://$StorageAccountName.blob.core.windows.net/$StorageContainerName/stub.txt"
$body = "Hello world"
$xmsdate = (get-date -format r).ToString()
$headers.Add("x-ms-date",$xmsdate)
$bytes = ([System.Text.Encoding]::UTF8.GetBytes($body))
$contentLength = $bytes.length
$headers.Add("Content-Length","$contentLength")
$headers.Add("x-ms-blob-type","BlockBlob")
$signatureString = "$method$([char]10)$([char]10)$([char]10)$contentLength$([char]10)$([char]10)$([char]10)$([char]10)$([char]10)$([char]10)$([char]10)$([char]10)$([char]10)"
#Add CanonicalizedHeaders
$signatureString += "x-ms-blob-type:" + $headers["x-ms-blob-type"] + "$([char]10)"
$signatureString += "x-ms-date:" + $headers["x-ms-date"] + "$([char]10)"
$signatureString += "x-ms-version:" + $headers["x-ms-version"] + "$([char]10)"
#Add CanonicalizedResource
$uri = New-Object System.Uri -ArgumentList $url
$signatureString += "/" + $StorageAccountName + $uri.AbsolutePath
$dataToMac = [System.Text.Encoding]::UTF8.GetBytes($signatureString)
$accountKeyBytes = [System.Convert]::FromBase64String($StorageAccountKey)
$hmac = new-object System.Security.Cryptography.HMACSHA256((,$accountKeyBytes))
$signature = [System.Convert]::ToBase64String($hmac.ComputeHash($dataToMac))
$headers.Add("Authorization", "SharedKey " + $StorageAccountName + ":" + $signature);
write-host -fore green $signatureString
Invoke-RestMethod -Uri $Url -Method $method -headers $headers -Body $body
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment