Azure Storage: Authenticate with an Azure VM Managed Identity
See more Azure Cloud Storage Examples
Run on an Azure VM with a system-assigned or user-assigned managed identity. In storage.json supply account and optionally managed_client_id. Assign Storage Blob Data Reader (or a suitable data role) at the needed scope. The example obtains a Storage-audience token from IMDS and uses it for a Blob HEAD request. Do not send IMDS traffic through a proxy or make its destination user-controlled.
The token is saved to private qa_data/azure/storage-token.json with an expires_at timestamp for the related examples. Cache it until shortly before expiration, then repeat token acquisition. This flow does not use a refresh token. A 401 and a 403 need different investigation: token validity/audience versus data permissions or account policy. Allow time for RBAC changes to propagate.
Uses direct Chilkat.Rest requests and Azure public-cloud endpoints with verified HTTPS. REST requests select service version 2026-04-06. Create local input/output directories before running. Account-key examples read account and access_key string fields from private qa_data/azure/storage.json; do not embed secrets in distributed applications. Entra examples explain their own configuration. Keep credentials, bearer tokens, SAS URLs and diagnostic dumps private. Shared Key is supported only where allowed by account policy; Entra authentication is preferred where available.
Transport success and HTTP status are checked separately. Writes are not automatically replayed after an uncertain response. IDs, ETags, continuation markers and file sizes are strings; counters and explicitly bounded offsets use int. No webhook or web server is required.
Chilkat PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_TextCompare
oleobject loo_Config
long li_SameText1
oleobject loo_Identity
oleobject loo_Expires
long li_SameText2
string ls_TokenText
oleobject loo_Token
long li_SameText3
long li_SameText4
long li_Seconds
string ls_AccessToken
oleobject loo_Endpoint
string ls_ResourcePath
oleobject loo_Rest
oleobject loo_Connection
string ls_ResponseText
oleobject loo_Result
oleobject loo_Bearer
li_Success = 0
// StringBuilder compares text by value in every generated language.
loo_TextCompare = create oleobject
li_rc = loo_TextCompare.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
destroy loo_TextCompare
MessageBox("Error","Connecting to COM object failed")
return
end if
// Assumes Chilkat is unlocked. See Global Unlock Sample.
li_Success = 0
loo_Config = create oleobject
li_rc = loo_Config.ConnectToNewObject("Chilkat.JsonObject")
loo_Config.Strict = 1
li_Success = loo_Config.LoadFile("qa_data/azure/storage.json")
if li_Success <> 1 then
Write-Debug "Could not load private Azure configuration."
destroy loo_TextCompare
destroy loo_Config
return
end if
loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Config.StringOf("account"))
li_SameText1 = loo_TextCompare.ContentsEqual("",1)
if (loo_Config.JsonTypeOf("account") <> 1) OR (li_SameText1 = 1) then
Write-Debug "Supply the storage account name."
destroy loo_TextCompare
destroy loo_Config
return
end if
loo_Identity = create oleobject
li_rc = loo_Identity.ConnectToNewObject("Chilkat.Rest")
loo_Identity.ConnectTimeoutMs = 20000
loo_Identity.IdleTimeoutMs = 60000
loo_Expires = create oleobject
li_rc = loo_Expires.ConnectToNewObject("Chilkat.CkDateTime")
li_Success = loo_Expires.SetFromCurrentSystemTime()
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Config
destroy loo_Identity
destroy loo_Expires
return
end if
// Azure VM IMDS only: fixed link-local endpoint, direct connection, no proxy.
// App Service, Functions and Azure Arc have different identity endpoints.
li_Success = loo_Identity.Connect("169.254.169.254",80,0,0)
if li_Success <> 1 then
Write-Debug "IMDS is available only on an eligible Azure VM."
destroy loo_TextCompare
destroy loo_Config
destroy loo_Identity
destroy loo_Expires
return
end if
li_Success = loo_Identity.AddHeader("Metadata","true")
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Config
destroy loo_Identity
destroy loo_Expires
return
end if
li_Success = loo_Identity.AddQueryParam("api-version","2018-02-01")
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Config
destroy loo_Identity
destroy loo_Expires
return
end if
li_Success = loo_Identity.AddQueryParam("resource","https://storage.azure.com/")
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Config
destroy loo_Identity
destroy loo_Expires
return
end if
// Optional user-assigned identity. Omit for the VM's system-assigned identity.
if loo_Config.HasMember("managed_client_id") = 1 then
loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Config.StringOf("managed_client_id"))
li_SameText2 = loo_TextCompare.ContentsEqual("",1)
if (loo_Config.JsonTypeOf("managed_client_id") <> 1) OR (li_SameText2 = 1) then
Write-Debug "Invalid managed_client_id."
destroy loo_TextCompare
destroy loo_Config
destroy loo_Identity
destroy loo_Expires
return
end if
li_Success = loo_Identity.AddQueryParam("client_id",loo_Config.StringOf("managed_client_id"))
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Config
destroy loo_Identity
destroy loo_Expires
return
end if
end if
ls_TokenText = loo_Identity.FullRequestNoBody("GET","/metadata/identity/oauth2/token")
if loo_Identity.LastMethodSuccess <> 1 then
Write-Debug "Token request did not complete; inspect diagnostics privately."
destroy loo_TextCompare
destroy loo_Config
destroy loo_Identity
destroy loo_Expires
return
end if
if loo_Identity.ResponseStatusCode <> 200 then
Write-Debug "Token request failed; inspect the status privately, never print the response token."
destroy loo_TextCompare
destroy loo_Config
destroy loo_Identity
destroy loo_Expires
return
end if
loo_Token = create oleobject
li_rc = loo_Token.ConnectToNewObject("Chilkat.JsonObject")
loo_Token.Strict = 1
li_Success = loo_Token.Load(ls_TokenText)
if li_Success <> 1 then
Write-Debug "Expected token JSON."
destroy loo_TextCompare
destroy loo_Config
destroy loo_Identity
destroy loo_Expires
destroy loo_Token
return
end if
loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Token.StringOf("access_token"))
li_SameText3 = loo_TextCompare.ContentsEqual("",1)
loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Token.StringOf("token_type"))
li_SameText4 = loo_TextCompare.ContentsEqual("Bearer",1)
if (loo_Token.JsonTypeOf("access_token") <> 1) OR (li_SameText3 = 1) OR (li_SameText4 <> 1) then
Write-Debug "Missing bearer token."
destroy loo_TextCompare
destroy loo_Config
destroy loo_Identity
destroy loo_Expires
destroy loo_Token
return
end if
li_Seconds = loo_Token.IntOf("expires_in")
if (li_Seconds < 120) OR (li_Seconds > 86400) then
Write-Debug "Unexpected token lifetime."
destroy loo_TextCompare
destroy loo_Config
destroy loo_Identity
destroy loo_Expires
destroy loo_Token
return
end if
li_Success = loo_Expires.AddSeconds(li_Seconds)
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Config
destroy loo_Identity
destroy loo_Expires
destroy loo_Token
return
end if
li_Success = loo_Token.UpdateString("expires_at",loo_Expires.GetAsTimestamp(0))
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Config
destroy loo_Identity
destroy loo_Expires
destroy loo_Token
return
end if
li_Success = loo_Token.WriteFile("qa_data/azure/storage-token.json")
if li_Success <> 1 then
Write-Debug "Could not persist token privately."
destroy loo_TextCompare
destroy loo_Config
destroy loo_Identity
destroy loo_Expires
destroy loo_Token
return
end if
ls_AccessToken = loo_Token.StringOf("access_token")
loo_Endpoint = create oleobject
li_rc = loo_Endpoint.ConnectToNewObject("Chilkat.StringBuilder")
li_Success = loo_Endpoint.Append(loo_Config.StringOf("account"))
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Config
destroy loo_Identity
destroy loo_Expires
destroy loo_Token
destroy loo_Endpoint
return
end if
li_Success = loo_Endpoint.Append(".blob.core.windows.net")
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Config
destroy loo_Identity
destroy loo_Expires
destroy loo_Token
destroy loo_Endpoint
return
end if
// resourcePath is already URL-encoded. Encode each path segment once when adapting it.
// Keep slash separators; names and paths are case-sensitive.
ls_ResourcePath = "/examplecontainer/example.bin"
loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest")
loo_Rest.ConnectTimeoutMs = 20000
loo_Rest.IdleTimeoutMs = 60000
loo_Connection = create oleobject
li_rc = loo_Connection.ConnectToNewObject("Chilkat.Socket")
loo_Connection.RequireSslCertVerify = 1
loo_Connection.MaxReadIdleMs = 60000
loo_Connection.MaxSendIdleMs = 60000
li_Success = loo_Connection.Connect(loo_Endpoint.GetAsString(),443,1,20000)
if li_Success <> 1 then
Write-Debug "TLS connection failed; inspect diagnostics privately."
destroy loo_TextCompare
destroy loo_Config
destroy loo_Identity
destroy loo_Expires
destroy loo_Token
destroy loo_Endpoint
destroy loo_Rest
destroy loo_Connection
return
end if
// Do not replay a write automatically after losing its response.
li_Success = loo_Rest.UseConnection(loo_Connection,0)
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Config
destroy loo_Identity
destroy loo_Expires
destroy loo_Token
destroy loo_Endpoint
destroy loo_Rest
destroy loo_Connection
return
end if
loo_Result = create oleobject
li_rc = loo_Result.ConnectToNewObject("Chilkat.Xml")
loo_Result.Strict = 1
li_Success = loo_Rest.AddHeader("x-ms-version","2026-04-06")
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Config
destroy loo_Identity
destroy loo_Expires
destroy loo_Token
destroy loo_Endpoint
destroy loo_Rest
destroy loo_Connection
destroy loo_Result
return
end if
loo_Bearer = create oleobject
li_rc = loo_Bearer.ConnectToNewObject("Chilkat.StringBuilder")
li_Success = loo_Bearer.Append("Bearer ")
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Config
destroy loo_Identity
destroy loo_Expires
destroy loo_Token
destroy loo_Endpoint
destroy loo_Rest
destroy loo_Connection
destroy loo_Result
destroy loo_Bearer
return
end if
li_Success = loo_Bearer.Append(ls_AccessToken)
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Config
destroy loo_Identity
destroy loo_Expires
destroy loo_Token
destroy loo_Endpoint
destroy loo_Rest
destroy loo_Connection
destroy loo_Result
destroy loo_Bearer
return
end if
li_Success = loo_Rest.AddHeader("Authorization",loo_Bearer.GetAsString())
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Config
destroy loo_Identity
destroy loo_Expires
destroy loo_Token
destroy loo_Endpoint
destroy loo_Rest
destroy loo_Connection
destroy loo_Result
destroy loo_Bearer
return
end if
ls_ResponseText = loo_Rest.FullRequestNoBody("HEAD",ls_ResourcePath)
if loo_Rest.LastMethodSuccess <> 1 then
Write-Debug "Request did not complete; outcome may be uncertain."
destroy loo_TextCompare
destroy loo_Config
destroy loo_Identity
destroy loo_Expires
destroy loo_Token
destroy loo_Endpoint
destroy loo_Rest
destroy loo_Connection
destroy loo_Result
destroy loo_Bearer
return
end if
if loo_Rest.ResponseStatusCode <> 200 then
Write-Debug "HTTP status: " + string(loo_Rest.ResponseStatusCode)
Write-Debug "Azure error code: " + loo_Rest.ResponseHdrByName("x-ms-error-code")
Write-Debug "Request ID: " + loo_Rest.ResponseHdrByName("x-ms-request-id")
// Inspect any error body privately; do not log credentials, SAS URLs or payloads.
destroy loo_TextCompare
destroy loo_Config
destroy loo_Identity
destroy loo_Expires
destroy loo_Token
destroy loo_Endpoint
destroy loo_Rest
destroy loo_Connection
destroy loo_Result
destroy loo_Bearer
return
end if
Write-Debug "Storage token obtained and blob properties read."
Write-Debug "ETag: " + loo_Rest.ResponseHdrByName("ETag")
destroy loo_TextCompare
destroy loo_Config
destroy loo_Identity
destroy loo_Expires
destroy loo_Token
destroy loo_Endpoint
destroy loo_Rest
destroy loo_Connection
destroy loo_Result
destroy loo_Bearer