Dismiss Notice

Register now to be one of the first members of this SharePoint Community! Click here it just takes seconds!

Dismiss Notice
Welcome Guest from Country Flag

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

Discussion in 'Official Microsoft News' started by Maheshk [MSFT], Apr 6, 2017.

Thread Status:
Not open for further replies.
  1. Maheshk [MSFT]

    Maheshk [MSFT] Guest

    Blog Posts:
    0
    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

    $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

    https://docs.microsoft.com/en-us/azure/storage/storage-introduction#storage-apis-libraries-and-tools

    Continue reading...
     
Thread Status:
Not open for further replies.

Share This Page

LiveZilla Live Chat Software