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 Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loTextCompare
LOCAL loConfig
LOCAL lnSameText1
LOCAL lnSameText2
LOCAL loEndpoint
LOCAL lcResourcePath
LOCAL loRest
LOCAL loAzAuth
LOCAL loConnection
LOCAL lcResponseText
LOCAL loResult
LOCAL loSourceUrl
LOCAL lcCopyId
LOCAL lnSameText3
LOCAL lnPolls
LOCAL lnCompleted
LOCAL lnSameText4
LOCAL lcCopyStatus
LOCAL lnSameText5
LOCAL lnSameText6
lnSuccess = 0
* StringBuilder compares text by value in every generated language.
loTextCompare = CreateObject('Chilkat.StringBuilder')
* Assumes Chilkat is unlocked. See Global Unlock Sample.
lnSuccess = 0
loConfig = CreateObject('Chilkat.JsonObject')
loConfig.Strict = 1
lnSuccess = loConfig.LoadFile("qa_data/azure/storage.json")
IF (lnSuccess <> 1) THEN
? "Could not load private Azure configuration."
RELEASE loTextCompare
RELEASE loConfig
CANCEL
ENDIF
loTextCompare.Clear()
loTextCompare.SetString(loConfig.StringOf("account"))
lnSameText1 = loTextCompare.ContentsEqual("",1)
IF ((loConfig.JsonTypeOf("account") <> 1) OR (lnSameText1 = 1)) THEN
? "Supply the storage account name."
RELEASE loTextCompare
RELEASE loConfig
CANCEL
ENDIF
* access_key is the base64 account key, not a connection string or SAS.
loTextCompare.Clear()
loTextCompare.SetString(loConfig.StringOf("access_key"))
lnSameText2 = loTextCompare.ContentsEqual("",1)
IF ((loConfig.JsonTypeOf("access_key") <> 1) OR (lnSameText2 = 1)) THEN
? "Supply the base64 account access key."
RELEASE loTextCompare
RELEASE loConfig
CANCEL
ENDIF
loEndpoint = CreateObject('Chilkat.StringBuilder')
lnSuccess = loEndpoint.Append(loConfig.StringOf("account"))
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
CANCEL
ENDIF
lnSuccess = loEndpoint.Append(".blob.core.windows.net")
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
CANCEL
ENDIF
* resourcePath is already URL-encoded. Encode each path segment once when adapting it.
* Keep slash separators; names and paths are case-sensitive.
lcResourcePath = "/examplecontainer/example-copy.bin"
loRest = CreateObject('Chilkat.Rest')
loRest.ConnectTimeoutMs = 20000
loRest.IdleTimeoutMs = 60000
loAzAuth = CreateObject('Chilkat.AuthAzureStorage')
loAzAuth.Account = loConfig.StringOf("account")
loAzAuth.AccessKey = loConfig.StringOf("access_key")
loAzAuth.Service = "Blob"
loAzAuth.Scheme = "SharedKey"
loAzAuth.XMsVersion = "2026-04-06"
lnSuccess = loRest.SetAuthAzureStorage(loAzAuth)
IF (lnSuccess <> 1) THEN
? "Could not configure Shared Key signing."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
CANCEL
ENDIF
loConnection = CreateObject('Chilkat.Socket')
loConnection.RequireSslCertVerify = 1
loConnection.MaxReadIdleMs = 60000
loConnection.MaxSendIdleMs = 60000
lnSuccess = loConnection.Connect(loEndpoint.GetAsString(),443,1,20000)
IF (lnSuccess <> 1) THEN
? "TLS connection failed; inspect diagnostics privately."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
CANCEL
ENDIF
* Do not replay a write automatically after losing its response.
lnSuccess = loRest.UseConnection(loConnection,0)
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
CANCEL
ENDIF
loResult = CreateObject('Chilkat.Xml')
loResult.Strict = 1
loSourceUrl = CreateObject('Chilkat.StringBuilder')
lnSuccess = loSourceUrl.Append("https://")
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResult
RELEASE loSourceUrl
CANCEL
ENDIF
lnSuccess = loSourceUrl.Append(loEndpoint.GetAsString())
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResult
RELEASE loSourceUrl
CANCEL
ENDIF
lnSuccess = loSourceUrl.Append("/examplecontainer/example.bin")
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResult
RELEASE loSourceUrl
CANCEL
ENDIF
lnSuccess = loRest.AddHeader("x-ms-copy-source",loSourceUrl.GetAsString())
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResult
RELEASE loSourceUrl
CANCEL
ENDIF
lnSuccess = loRest.AddHeader("If-None-Match","*")
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResult
RELEASE loSourceUrl
CANCEL
ENDIF
lcResponseText = loRest.FullRequestNoBody("PUT",lcResourcePath)
IF (loRest.LastMethodSuccess <> 1) THEN
? "Request did not complete; outcome may be uncertain."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResult
RELEASE loSourceUrl
CANCEL
ENDIF
IF (loRest.ResponseStatusCode <> 202) THEN
? "HTTP status: " + STR(loRest.ResponseStatusCode)
? "Azure error code: " + loRest.ResponseHdrByName("x-ms-error-code")
? "Request ID: " + loRest.ResponseHdrByName("x-ms-request-id")
* Inspect any error body privately; do not log credentials, SAS URLs or payloads.
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResult
RELEASE loSourceUrl
CANCEL
ENDIF
lcCopyId = loRest.ResponseHdrByName("x-ms-copy-id")
loTextCompare.Clear()
loTextCompare.SetString(lcCopyId)
lnSameText3 = loTextCompare.ContentsEqual("",1)
IF (lnSameText3 = 1) THEN
? "Copy accepted without a copy ID; inspect before retrying."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResult
RELEASE loSourceUrl
CANCEL
ENDIF
? "Copy ID: " + lcCopyId
* Save copyId and destination in application state if polling must survive restart.
lnSuccess = loRest.ClearAllHeaders()
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResult
RELEASE loSourceUrl
CANCEL
ENDIF
lnSuccess = loRest.ClearAllQueryParams()
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResult
RELEASE loSourceUrl
CANCEL
ENDIF
lnPolls = 0
lnCompleted = 0
DO WHILE ((lnPolls < 20) AND (lnCompleted <> 1))
loRest.Disconnect(10)
lnSuccess = loConnection.Connect(loEndpoint.GetAsString(),443,1,20000)
IF (lnSuccess <> 1) THEN
? "TLS reconnection failed."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResult
RELEASE loSourceUrl
CANCEL
ENDIF
lnSuccess = loRest.UseConnection(loConnection,0)
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResult
RELEASE loSourceUrl
CANCEL
ENDIF
lcResponseText = loRest.FullRequestNoBody("HEAD",lcResourcePath)
IF (loRest.LastMethodSuccess <> 1) THEN
? "Request did not complete; outcome may be uncertain."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResult
RELEASE loSourceUrl
CANCEL
ENDIF
IF (loRest.ResponseStatusCode <> 200) THEN
? "HTTP status: " + STR(loRest.ResponseStatusCode)
? "Azure error code: " + loRest.ResponseHdrByName("x-ms-error-code")
? "Request ID: " + loRest.ResponseHdrByName("x-ms-request-id")
* Inspect any error body privately; do not log credentials, SAS URLs or payloads.
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResult
RELEASE loSourceUrl
CANCEL
ENDIF
loTextCompare.Clear()
loTextCompare.SetString(loRest.ResponseHdrByName("x-ms-copy-id"))
lnSameText4 = loTextCompare.ContentsEqual(lcCopyId,1)
IF (lnSameText4 <> 1) THEN
? "Destination no longer reports this copy ID."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResult
RELEASE loSourceUrl
CANCEL
ENDIF
lcCopyStatus = loRest.ResponseHdrByName("x-ms-copy-status")
loTextCompare.Clear()
loTextCompare.SetString(lcCopyStatus)
lnSameText5 = loTextCompare.ContentsEqual("success",1)
IF (lnSameText5 = 1) THEN
lnCompleted = 1
ELSE
loTextCompare.Clear()
loTextCompare.SetString(lcCopyStatus)
lnSameText6 = loTextCompare.ContentsEqual("pending",1)
IF (lnSameText6 <> 1) THEN
? "Copy failed, was aborted, or returned an unexpected status; inspect copy diagnostics privately."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResult
RELEASE loSourceUrl
CANCEL
ENDIF
* Pause between polls; the copy can outlast this example's bounded wait.
loConnection.SleepMs(1000)
ENDIF
lnPolls = lnPolls + 1
ENDDO
IF (lnCompleted <> 1) THEN
? "Copy is still pending. Continue polling this ID; do not start another copy."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResult
RELEASE loSourceUrl
CANCEL
ENDIF
? "Copy completed."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loEndpoint
RELEASE loRest
RELEASE loAzAuth
RELEASE loConnection
RELEASE loResult
RELEASE loSourceUrl