Azure Blob: Copy a Blob and Track Completion
See more Azure Cloud Storage Examples
Copy a blob within the same account without downloading it into the application. Shared Key authorizes the same-account source and destination. If-None-Match: * prevents replacing an existing destination. Cross-account sources need separate authorization, often a read SAS; never log that URL.
A 202 means accepted, not completed. Retain the returned copy ID and poll destination properties until success, failure or abort. The example waits for at most 20 polls; pending at that point is not success and does not cancel the copy. On restart continue tracking the original ID, rather than issuing another copy. Account type, archive tier, leases and destination conditions can affect copy eligibility.
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
long li_SameText2
oleobject loo_Endpoint
string ls_ResourcePath
oleobject loo_Rest
oleobject loo_AzAuth
oleobject loo_Connection
string ls_ResponseText
oleobject loo_Result
oleobject loo_SourceUrl
string ls_CopyId
long li_SameText3
long li_Polls
long li_Completed
long li_SameText4
string ls_CopyStatus
long li_SameText5
long li_SameText6
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
// access_key is the base64 account key, not a connection string or SAS.
loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Config.StringOf("access_key"))
li_SameText2 = loo_TextCompare.ContentsEqual("",1)
if (loo_Config.JsonTypeOf("access_key") <> 1) OR (li_SameText2 = 1) then
Write-Debug "Supply the base64 account access key."
destroy loo_TextCompare
destroy loo_Config
return
end if
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_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_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-copy.bin"
loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest")
loo_Rest.ConnectTimeoutMs = 20000
loo_Rest.IdleTimeoutMs = 60000
loo_AzAuth = create oleobject
li_rc = loo_AzAuth.ConnectToNewObject("Chilkat.AuthAzureStorage")
loo_AzAuth.Account = loo_Config.StringOf("account")
loo_AzAuth.AccessKey = loo_Config.StringOf("access_key")
loo_AzAuth.Service = "Blob"
loo_AzAuth.Scheme = "SharedKey"
loo_AzAuth.XMsVersion = "2026-04-06"
li_Success = loo_Rest.SetAuthAzureStorage(loo_AzAuth)
if li_Success <> 1 then
Write-Debug "Could not configure Shared Key signing."
destroy loo_TextCompare
destroy loo_Config
destroy loo_Endpoint
destroy loo_Rest
destroy loo_AzAuth
return
end if
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_Endpoint
destroy loo_Rest
destroy loo_AzAuth
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_Endpoint
destroy loo_Rest
destroy loo_AzAuth
destroy loo_Connection
return
end if
loo_Result = create oleobject
li_rc = loo_Result.ConnectToNewObject("Chilkat.Xml")
loo_Result.Strict = 1
loo_SourceUrl = create oleobject
li_rc = loo_SourceUrl.ConnectToNewObject("Chilkat.StringBuilder")
li_Success = loo_SourceUrl.Append("https://")
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Config
destroy loo_Endpoint
destroy loo_Rest
destroy loo_AzAuth
destroy loo_Connection
destroy loo_Result
destroy loo_SourceUrl
return
end if
li_Success = loo_SourceUrl.Append(loo_Endpoint.GetAsString())
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Config
destroy loo_Endpoint
destroy loo_Rest
destroy loo_AzAuth
destroy loo_Connection
destroy loo_Result
destroy loo_SourceUrl
return
end if
li_Success = loo_SourceUrl.Append("/examplecontainer/example.bin")
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Config
destroy loo_Endpoint
destroy loo_Rest
destroy loo_AzAuth
destroy loo_Connection
destroy loo_Result
destroy loo_SourceUrl
return
end if
li_Success = loo_Rest.AddHeader("x-ms-copy-source",loo_SourceUrl.GetAsString())
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Config
destroy loo_Endpoint
destroy loo_Rest
destroy loo_AzAuth
destroy loo_Connection
destroy loo_Result
destroy loo_SourceUrl
return
end if
li_Success = loo_Rest.AddHeader("If-None-Match","*")
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Config
destroy loo_Endpoint
destroy loo_Rest
destroy loo_AzAuth
destroy loo_Connection
destroy loo_Result
destroy loo_SourceUrl
return
end if
ls_ResponseText = loo_Rest.FullRequestNoBody("PUT",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_Endpoint
destroy loo_Rest
destroy loo_AzAuth
destroy loo_Connection
destroy loo_Result
destroy loo_SourceUrl
return
end if
if loo_Rest.ResponseStatusCode <> 202 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_Endpoint
destroy loo_Rest
destroy loo_AzAuth
destroy loo_Connection
destroy loo_Result
destroy loo_SourceUrl
return
end if
ls_CopyId = loo_Rest.ResponseHdrByName("x-ms-copy-id")
loo_TextCompare.Clear()
loo_TextCompare.SetString(ls_CopyId)
li_SameText3 = loo_TextCompare.ContentsEqual("",1)
if li_SameText3 = 1 then
Write-Debug "Copy accepted without a copy ID; inspect before retrying."
destroy loo_TextCompare
destroy loo_Config
destroy loo_Endpoint
destroy loo_Rest
destroy loo_AzAuth
destroy loo_Connection
destroy loo_Result
destroy loo_SourceUrl
return
end if
Write-Debug "Copy ID: " + ls_CopyId
// Save copyId and destination in application state if polling must survive restart.
li_Success = loo_Rest.ClearAllHeaders()
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Config
destroy loo_Endpoint
destroy loo_Rest
destroy loo_AzAuth
destroy loo_Connection
destroy loo_Result
destroy loo_SourceUrl
return
end if
li_Success = loo_Rest.ClearAllQueryParams()
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Config
destroy loo_Endpoint
destroy loo_Rest
destroy loo_AzAuth
destroy loo_Connection
destroy loo_Result
destroy loo_SourceUrl
return
end if
li_Polls = 0
li_Completed = 0
do while ((li_Polls < 20) AND (li_Completed <> 1))
loo_Rest.Disconnect(10)
li_Success = loo_Connection.Connect(loo_Endpoint.GetAsString(),443,1,20000)
if li_Success <> 1 then
Write-Debug "TLS reconnection failed."
destroy loo_TextCompare
destroy loo_Config
destroy loo_Endpoint
destroy loo_Rest
destroy loo_AzAuth
destroy loo_Connection
destroy loo_Result
destroy loo_SourceUrl
return
end if
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_Endpoint
destroy loo_Rest
destroy loo_AzAuth
destroy loo_Connection
destroy loo_Result
destroy loo_SourceUrl
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_Endpoint
destroy loo_Rest
destroy loo_AzAuth
destroy loo_Connection
destroy loo_Result
destroy loo_SourceUrl
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_Endpoint
destroy loo_Rest
destroy loo_AzAuth
destroy loo_Connection
destroy loo_Result
destroy loo_SourceUrl
return
end if
loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Rest.ResponseHdrByName("x-ms-copy-id"))
li_SameText4 = loo_TextCompare.ContentsEqual(ls_CopyId,1)
if li_SameText4 <> 1 then
Write-Debug "Destination no longer reports this copy ID."
destroy loo_TextCompare
destroy loo_Config
destroy loo_Endpoint
destroy loo_Rest
destroy loo_AzAuth
destroy loo_Connection
destroy loo_Result
destroy loo_SourceUrl
return
end if
ls_CopyStatus = loo_Rest.ResponseHdrByName("x-ms-copy-status")
loo_TextCompare.Clear()
loo_TextCompare.SetString(ls_CopyStatus)
li_SameText5 = loo_TextCompare.ContentsEqual("success",1)
if li_SameText5 = 1 then
li_Completed = 1
else
loo_TextCompare.Clear()
loo_TextCompare.SetString(ls_CopyStatus)
li_SameText6 = loo_TextCompare.ContentsEqual("pending",1)
if li_SameText6 <> 1 then
Write-Debug "Copy failed, was aborted, or returned an unexpected status; inspect copy diagnostics privately."
destroy loo_TextCompare
destroy loo_Config
destroy loo_Endpoint
destroy loo_Rest
destroy loo_AzAuth
destroy loo_Connection
destroy loo_Result
destroy loo_SourceUrl
return
end if
// Pause between polls; the copy can outlast this example's bounded wait.
loo_Connection.SleepMs(1000)
end if
li_Polls = li_Polls + 1
loop
if li_Completed <> 1 then
Write-Debug "Copy is still pending. Continue polling this ID; do not start another copy."
destroy loo_TextCompare
destroy loo_Config
destroy loo_Endpoint
destroy loo_Rest
destroy loo_AzAuth
destroy loo_Connection
destroy loo_Result
destroy loo_SourceUrl
return
end if
Write-Debug "Copy completed."
destroy loo_TextCompare
destroy loo_Config
destroy loo_Endpoint
destroy loo_Rest
destroy loo_AzAuth
destroy loo_Connection
destroy loo_Result
destroy loo_SourceUrl