Udostępnij za pośrednictwem


[Azure Storage] How to call Storage API’s from Powershell (without SDK)

Recently I had this ask from a partner who wanted sample code for making REST API call to storage without our SDK’s route. Though we have array of SDK’s supporting many languages, still he wanted to make clean REST calls without SDK’s. It some time to understand and create this proof of concept. So sharing here to easy reference. Hope this helps in some way..

$accountname="your_storage_accname"

$key = "acc_key"

$container="container_name"

#file to create

$blkblob="samplefile.log"

$f = "C:\\temp\\samplefile.log"

$BlobOperation = "PUT"

$body = (Get-Content -Path $f -Raw)

$filelen = $body.Length

#added this per comments in the below blog post.

$filelen = (Get-ChildItem -File $f).Length

$RESTAPI_URL = "https://$accountname.blob.core.windows.net/$container/$blkblob";

$date=(Get-Date).ToUniversalTime()

$datestr=$date.ToString("R");

$datestr2=$date.ToString("s")+"Z";

$strtosign = "$BlobOperation`n`n`n$filelen`n`n`n`n`n`n`n`n`nx-ms-blob-type:BlockBlob`nx-ms-date:$datestr`nx-ms-version:2015-04-05`n/"

$strtosign = $strtosign + $accountname + "/"

$strtosign = $strtosign + $container

$strtosign = $strtosign + "/" +$blkblob

 

write-host $strtosign

 

[byte[]]$dataBytes = ([System.Text.Encoding]::UTF8).GetBytes($strtosign)

$hmacsha256 = New-Object System.Security.Cryptography.HMACSHA256

$hmacsha256.Key = [Convert]::FromBase64String($key)

$sig = [Convert]::ToBase64String($hmacsha256.ComputeHash($dataBytes))

$authhdr = "SharedKey $accountname`:$sig"

 

write-host $authhdr

 

$RequestHeader = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"

 

$RequestHeader.Add("Authorization", $authhdr)

$RequestHeader.Add("x-ms-date", $datestr)

$RequestHeader.Add("x-ms-version", "2015-04-05")

$RequestHeader.Add("x-ms-blob-type","BlockBlob")

 

#create a new PS object to hold the response JSON

$RESTResponse = New-Object PSObject;

$RESTResponse = (Invoke-RestMethod -Uri $RESTAPI_URL -Method put -Headers $RequestHeader -InFile $f);

 

write-host $RESTResponse

write-host "# Success !!! uploaded the file >>" $RESTAPI_URL

-------------------------------------------------------------------------------------------------------------------------------------------------------------

$accountname="your_storage_accname"

$key = "acc_key"

$container="container_name"

$blkblob="file_for_deletion"

$BlobOperation = "DELETE"

 

$RESTAPI_URL = "https://$accountname.blob.core.windows.net/$container/$blkblob";

$date=(Get-Date).ToUniversalTime()

$datestr=$date.ToString("R");

$datestr2=$date.ToString("s")+"Z";

 

$strtosign = "$BlobOperation`n`n`n`n`n`n`n`n`n`n`n`nx-ms-blob-type:BlockBlob`nx-ms-date:$datestr`nx-ms-version:2015-04-05`n/"

 

$strtosign = $strtosign + $accountname + "/"

$strtosign = $strtosign + $container

$strtosign = $strtosign + "/" +$blkblob

write-host $strtosign

 

[byte[]]$dataBytes = ([System.Text.Encoding]::UTF8).GetBytes($strtosign)

$hmacsha256 = New-Object System.Security.Cryptography.HMACSHA256

$hmacsha256.Key = [Convert]::FromBase64String($key)

$sig = [Convert]::ToBase64String($hmacsha256.ComputeHash($dataBytes))

$authhdr = "SharedKey $accountname`:$sig"

write-host $authhdr

 

$RequestHeader = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"

$RequestHeader.Add("Authorization", $authhdr)

$RequestHeader.Add("x-ms-date", $datestr)

$RequestHeader.Add("x-ms-version", "2015-04-05")

$RequestHeader.Add("x-ms-blob-type","BlockBlob")

$RESTResponse = New-Object PSObject;

write-host $RESTAPI_URL

 

$RESTResponse = (Invoke-RestMethod -Uri $RESTAPI_URL -Method Delete -Headers $RequestHeader);

 

write-host "# Success !!! deleted the input file >>" $RESTAPI_URL

reference:-

https://dzone.com/articles/examples-windows-azure-storage

/en-us/azure/storage/storage-introduction#storage-apis-libraries-and-tools

Comments

  • Anonymous
    November 13, 2017
    $body = (Get-Content -Path $f -Raw)$filelen = $body.Length returns the number of rows in the filechange to $filelen = (Get-ChildItem -File $f).Lengthfor size of file
    • Anonymous
      November 13, 2017
      Thanks Ilan.
      • Anonymous
        November 13, 2017
        Thank you, you saved me 3 hours of understanding how the authentication hashing works =)