Sample code for 30+ languages & platforms
Tcl Requires Chilkat v11.6.0+

Azure Blob: Download to a File with Streaming

See more Azure Cloud Storage Examples

Read response headers before writing bytes. Only HTTP 200 is streamed into a staging file, then renamed after completion. The final filename must not already exist; create qa_output first. An interrupted transfer can leave a .part file. This is a fresh download, not a resumable transfer.

Uses direct Chilkat REST calls with HTTPS certificate verification. Shared Key examples load a private qa_data/azure/storage.json file containing string fields account and access_key (the base64 account key). SAS consumers need only account there. Create local input/output directories before running. Examples target Azure public-cloud endpoints; adapt hostnames for other clouds or custom endpoints.

REST examples pin x-ms-version: 2026-04-06, and SAS generation uses the same service version. Complete-URL downloads use the supplied SAS version. This is a fully deployed version documented by Microsoft at review time. Older supported versions are not automatically obsolete. Transport completion and HTTP success are checked separately; writes are not automatically replayed after a lost response. Keep diagnostic dumps, keys and SAS URLs private.

Shared Key remains supported where allowed by account policy. Microsoft recommends Microsoft Entra ID, preferably managed identities where available, and user delegation SAS for delegated Blob access. Account-key SAS and Shared Key requests can be rejected when Shared Key access is disabled; these samples do not bypass that policy. See Azure data authorization.

Microsoft get blob reference.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  StringBuilder compares text by value in every generated language.
set textCompare [new_CkStringBuilder]

#  Assumes Chilkat is unlocked. See Global Unlock Sample.
set success 0
set config [new_CkJsonObject]

CkJsonObject_put_Strict $config 1
set success [CkJsonObject_LoadFile $config "qa_data/azure/storage.json"]
if {$success != 1} then {
    puts "Could not load private Azure configuration."
    delete_CkStringBuilder $textCompare
    delete_CkJsonObject $config
    exit
}

CkStringBuilder_Clear $textCompare
CkStringBuilder_SetString $textCompare [CkJsonObject_stringOf $config "account"]
set sameText1 [CkStringBuilder_ContentsEqual $textCompare "" 1]
if {expr [[CkJsonObject_JsonTypeOf $config "account"] != 1]  ||  [$sameText1 == 1]} then {
    puts "Supply the storage account name."
    delete_CkStringBuilder $textCompare
    delete_CkJsonObject $config
    exit
}

#  access_key is the base64 account key, not a connection string or SAS.
CkStringBuilder_Clear $textCompare
CkStringBuilder_SetString $textCompare [CkJsonObject_stringOf $config "access_key"]
set sameText2 [CkStringBuilder_ContentsEqual $textCompare "" 1]
if {expr [[CkJsonObject_JsonTypeOf $config "access_key"] != 1]  ||  [$sameText2 == 1]} then {
    puts "Supply the base64 account access key."
    delete_CkStringBuilder $textCompare
    delete_CkJsonObject $config
    exit
}

set endpoint [new_CkStringBuilder]

set success [CkStringBuilder_Append $endpoint [CkJsonObject_stringOf $config "account"]]
if {$success != 1} then {
    puts "Could not prepare the request."
    delete_CkStringBuilder $textCompare
    delete_CkJsonObject $config
    delete_CkStringBuilder $endpoint
    exit
}

set success [CkStringBuilder_Append $endpoint ".blob.core.windows.net"]
if {$success != 1} then {
    puts "Could not prepare the request."
    delete_CkStringBuilder $textCompare
    delete_CkJsonObject $config
    delete_CkStringBuilder $endpoint
    exit
}

#  resourcePath is already URL-encoded. Encode each path segment once when adapting it.
#  Keep slash separators; names and paths are case-sensitive.
set resourcePath "/examplecontainer/example.bin"
set rest [new_CkRest]

CkRest_put_ConnectTimeoutMs $rest 20000
CkRest_put_IdleTimeoutMs $rest 60000
set azAuth [new_CkAuthAzureStorage]

CkAuthAzureStorage_put_Account $azAuth [CkJsonObject_stringOf $config "account"]
CkAuthAzureStorage_put_AccessKey $azAuth [CkJsonObject_stringOf $config "access_key"]
CkAuthAzureStorage_put_Service $azAuth "Blob"
CkAuthAzureStorage_put_Scheme $azAuth "SharedKey"
CkAuthAzureStorage_put_XMsVersion $azAuth "2026-04-06"
set success [CkRest_SetAuthAzureStorage $rest $azAuth]
if {$success != 1} then {
    puts "Could not configure Shared Key signing."
    delete_CkStringBuilder $textCompare
    delete_CkJsonObject $config
    delete_CkStringBuilder $endpoint
    delete_CkRest $rest
    delete_CkAuthAzureStorage $azAuth
    exit
}

set connection [new_CkSocket]

CkSocket_put_RequireSslCertVerify $connection 1
CkSocket_put_MaxReadIdleMs $connection 60000
CkSocket_put_MaxSendIdleMs $connection 60000
set success [CkSocket_Connect $connection [CkStringBuilder_getAsString $endpoint] 443 1 20000]
if {$success != 1} then {
    puts "TLS connection failed; inspect diagnostics privately."
    delete_CkStringBuilder $textCompare
    delete_CkJsonObject $config
    delete_CkStringBuilder $endpoint
    delete_CkRest $rest
    delete_CkAuthAzureStorage $azAuth
    delete_CkSocket $connection
    exit
}

#  Do not replay a write automatically after losing its response.
set success [CkRest_UseConnection $rest $connection 0]
if {$success != 1} then {
    puts "Could not prepare the request."
    delete_CkStringBuilder $textCompare
    delete_CkJsonObject $config
    delete_CkStringBuilder $endpoint
    delete_CkRest $rest
    delete_CkAuthAzureStorage $azAuth
    delete_CkSocket $connection
    exit
}

set result [new_CkXml]

CkXml_put_Strict $result 1
#  Use a new destination and a staging file. A failed transfer is never a completed output.
set files [new_CkFileAccess]

if {[CkFileAccess_FileExists $files "qa_output/example.bin"] == 1} then {
    puts "Choose a new output filename."
    delete_CkStringBuilder $textCompare
    delete_CkJsonObject $config
    delete_CkStringBuilder $endpoint
    delete_CkRest $rest
    delete_CkAuthAzureStorage $azAuth
    delete_CkSocket $connection
    delete_CkXml $result
    delete_CkFileAccess $files
    exit
}

set sink [new_CkStream]

CkStream_put_SinkFile $sink "qa_output/example.bin.part"
set success [CkRest_SendReqNoBody $rest "GET" $resourcePath]
if {$success != 1} then {
    puts "Could not send download request."
    delete_CkStringBuilder $textCompare
    delete_CkJsonObject $config
    delete_CkStringBuilder $endpoint
    delete_CkRest $rest
    delete_CkAuthAzureStorage $azAuth
    delete_CkSocket $connection
    delete_CkXml $result
    delete_CkFileAccess $files
    delete_CkStream $sink
    exit
}

set httpStatus [CkRest_ReadResponseHeader $rest]
if {$httpStatus < 0} then {
    puts "Could not read response headers."
    delete_CkStringBuilder $textCompare
    delete_CkJsonObject $config
    delete_CkStringBuilder $endpoint
    delete_CkRest $rest
    delete_CkAuthAzureStorage $azAuth
    delete_CkSocket $connection
    delete_CkXml $result
    delete_CkFileAccess $files
    delete_CkStream $sink
    exit
}

if {[CkRest_get_ResponseStatusCode $rest] != 200} then {
    puts "HTTP status: [CkRest_get_ResponseStatusCode $rest]"
    puts "Azure error code: [CkRest_responseHdrByName $rest x-ms-error-code]"
    puts "Request ID: [CkRest_responseHdrByName $rest x-ms-request-id]"
    #  Inspect any error body privately; do not log credentials, SAS URLs or payloads.
    delete_CkStringBuilder $textCompare
    delete_CkJsonObject $config
    delete_CkStringBuilder $endpoint
    delete_CkRest $rest
    delete_CkAuthAzureStorage $azAuth
    delete_CkSocket $connection
    delete_CkXml $result
    delete_CkFileAccess $files
    delete_CkStream $sink
    exit
}

set success [CkRest_ReadRespBodyStream $rest $sink 0]
if {$success != 1} then {
    puts "Download incomplete; staging file is not a valid final output."
    delete_CkStringBuilder $textCompare
    delete_CkJsonObject $config
    delete_CkStringBuilder $endpoint
    delete_CkRest $rest
    delete_CkAuthAzureStorage $azAuth
    delete_CkSocket $connection
    delete_CkXml $result
    delete_CkFileAccess $files
    delete_CkStream $sink
    exit
}

set success [CkFileAccess_FileRename $files "qa_output/example.bin.part" "qa_output/example.bin"]
if {$success != 1} then {
    puts "Could not publish the downloaded file."
    delete_CkStringBuilder $textCompare
    delete_CkJsonObject $config
    delete_CkStringBuilder $endpoint
    delete_CkRest $rest
    delete_CkAuthAzureStorage $azAuth
    delete_CkSocket $connection
    delete_CkXml $result
    delete_CkFileAccess $files
    delete_CkStream $sink
    exit
}

puts "Download completed to qa_output/example.bin."

delete_CkStringBuilder $textCompare
delete_CkJsonObject $config
delete_CkStringBuilder $endpoint
delete_CkRest $rest
delete_CkAuthAzureStorage $azAuth
delete_CkSocket $connection
delete_CkXml $result
delete_CkFileAccess $files
delete_CkStream $sink