Azure Blob: Stream a File to a Block Blob
See more Azure Cloud Storage Examples
Stream a local file without buffering the whole file in application memory. This is one Put Blob request, not a resumable block upload.
If-None-Match: * makes creation fail with 412 when the blob exists. Remove the condition only for an intentional complete replacement; Put Blob overwrites content and metadata. With the selected API version, a single Put Blob supports up to 5,000 MiB, not the historical 64 MiB limit. Memory-based examples should use small inputs; use streaming or staged blocks for larger data.
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.
Chilkat PureBasic Downloads
IncludeFile "CkJsonObject.pb"
IncludeFile "CkSocket.pb"
IncludeFile "CkFileAccess.pb"
IncludeFile "CkAuthAzureStorage.pb"
IncludeFile "CkXml.pb"
IncludeFile "CkStream.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkRest.pb"
Procedure ChilkatExample()
success.i = 0
; StringBuilder compares text by value in every generated language.
textCompare.i = CkStringBuilder::ckCreate()
If textCompare.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Assumes Chilkat is unlocked. See Global Unlock Sample.
success = 0
config.i = CkJsonObject::ckCreate()
If config.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::setCkStrict(config, 1)
success = CkJsonObject::ckLoadFile(config,"qa_data/azure/storage.json")
If success <> 1
Debug "Could not load private Azure configuration."
CkStringBuilder::ckDispose(textCompare)
CkJsonObject::ckDispose(config)
ProcedureReturn
EndIf
CkStringBuilder::ckClear(textCompare)
CkStringBuilder::ckSetString(textCompare,CkJsonObject::ckStringOf(config,"account"))
sameText1.i = CkStringBuilder::ckContentsEqual(textCompare,"",1)
If (CkJsonObject::ckJsonTypeOf(config,"account") <> 1) OR (sameText1 = 1)
Debug "Supply the storage account name."
CkStringBuilder::ckDispose(textCompare)
CkJsonObject::ckDispose(config)
ProcedureReturn
EndIf
; access_key is the base64 account key, not a connection string or SAS.
CkStringBuilder::ckClear(textCompare)
CkStringBuilder::ckSetString(textCompare,CkJsonObject::ckStringOf(config,"access_key"))
sameText2.i = CkStringBuilder::ckContentsEqual(textCompare,"",1)
If (CkJsonObject::ckJsonTypeOf(config,"access_key") <> 1) OR (sameText2 = 1)
Debug "Supply the base64 account access key."
CkStringBuilder::ckDispose(textCompare)
CkJsonObject::ckDispose(config)
ProcedureReturn
EndIf
endpoint.i = CkStringBuilder::ckCreate()
If endpoint.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkStringBuilder::ckAppend(endpoint,CkJsonObject::ckStringOf(config,"account"))
If success <> 1
Debug "Could not prepare the request."
CkStringBuilder::ckDispose(textCompare)
CkJsonObject::ckDispose(config)
CkStringBuilder::ckDispose(endpoint)
ProcedureReturn
EndIf
success = CkStringBuilder::ckAppend(endpoint,".blob.core.windows.net")
If success <> 1
Debug "Could not prepare the request."
CkStringBuilder::ckDispose(textCompare)
CkJsonObject::ckDispose(config)
CkStringBuilder::ckDispose(endpoint)
ProcedureReturn
EndIf
; resourcePath is already URL-encoded. Encode each path segment once when adapting it.
; Keep slash separators; names and paths are case-sensitive.
resourcePath.s = "/examplecontainer/example.bin"
rest.i = CkRest::ckCreate()
If rest.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkRest::setCkConnectTimeoutMs(rest, 20000)
CkRest::setCkIdleTimeoutMs(rest, 60000)
azAuth.i = CkAuthAzureStorage::ckCreate()
If azAuth.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkAuthAzureStorage::setCkAccount(azAuth, CkJsonObject::ckStringOf(config,"account"))
CkAuthAzureStorage::setCkAccessKey(azAuth, CkJsonObject::ckStringOf(config,"access_key"))
CkAuthAzureStorage::setCkService(azAuth, "Blob")
CkAuthAzureStorage::setCkScheme(azAuth, "SharedKey")
CkAuthAzureStorage::setCkXMsVersion(azAuth, "2026-04-06")
success = CkRest::ckSetAuthAzureStorage(rest,azAuth)
If success <> 1
Debug "Could not configure Shared Key signing."
CkStringBuilder::ckDispose(textCompare)
CkJsonObject::ckDispose(config)
CkStringBuilder::ckDispose(endpoint)
CkRest::ckDispose(rest)
CkAuthAzureStorage::ckDispose(azAuth)
ProcedureReturn
EndIf
connection.i = CkSocket::ckCreate()
If connection.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkSocket::setCkRequireSslCertVerify(connection, 1)
CkSocket::setCkMaxReadIdleMs(connection, 60000)
CkSocket::setCkMaxSendIdleMs(connection, 60000)
success = CkSocket::ckConnect(connection,CkStringBuilder::ckGetAsString(endpoint),443,1,20000)
If success <> 1
Debug "TLS connection failed; inspect diagnostics privately."
CkStringBuilder::ckDispose(textCompare)
CkJsonObject::ckDispose(config)
CkStringBuilder::ckDispose(endpoint)
CkRest::ckDispose(rest)
CkAuthAzureStorage::ckDispose(azAuth)
CkSocket::ckDispose(connection)
ProcedureReturn
EndIf
; Do not replay a write automatically after losing its response.
success = CkRest::ckUseConnection(rest,connection,0)
If success <> 1
Debug "Could not prepare the request."
CkStringBuilder::ckDispose(textCompare)
CkJsonObject::ckDispose(config)
CkStringBuilder::ckDispose(endpoint)
CkRest::ckDispose(rest)
CkAuthAzureStorage::ckDispose(azAuth)
CkSocket::ckDispose(connection)
ProcedureReturn
EndIf
responseText.s
result.i = CkXml::ckCreate()
If result.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkXml::setCkStrict(result, 1)
success = CkRest::ckAddHeader(rest,"x-ms-blob-type","BlockBlob")
If success <> 1
Debug "Could not prepare the request."
CkStringBuilder::ckDispose(textCompare)
CkJsonObject::ckDispose(config)
CkStringBuilder::ckDispose(endpoint)
CkRest::ckDispose(rest)
CkAuthAzureStorage::ckDispose(azAuth)
CkSocket::ckDispose(connection)
CkXml::ckDispose(result)
ProcedureReturn
EndIf
success = CkRest::ckAddHeader(rest,"If-None-Match","*")
If success <> 1
Debug "Could not prepare the request."
CkStringBuilder::ckDispose(textCompare)
CkJsonObject::ckDispose(config)
CkStringBuilder::ckDispose(endpoint)
CkRest::ckDispose(rest)
CkAuthAzureStorage::ckDispose(azAuth)
CkSocket::ckDispose(connection)
CkXml::ckDispose(result)
ProcedureReturn
EndIf
input.i = CkFileAccess::ckCreate()
If input.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkFileAccess::ckOpenForRead(input,"qa_data/bin/sample.bin")
If success <> 1
Debug "Could not open local input."
CkFileAccess::ckFileClose(input)
CkStringBuilder::ckDispose(textCompare)
CkJsonObject::ckDispose(config)
CkStringBuilder::ckDispose(endpoint)
CkRest::ckDispose(rest)
CkAuthAzureStorage::ckDispose(azAuth)
CkSocket::ckDispose(connection)
CkXml::ckDispose(result)
CkFileAccess::ckDispose(input)
ProcedureReturn
EndIf
CkFileAccess::ckFileClose(input)
source.i = CkStream::ckCreate()
If source.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkStream::setCkSourceFile(source, "qa_data/bin/sample.bin")
responseText = CkRest::ckFullRequestStream(rest,"PUT",resourcePath,source)
If CkRest::ckLastMethodSuccess(rest) <> 1
Debug "Request did not complete; outcome may be uncertain."
CkStringBuilder::ckDispose(textCompare)
CkJsonObject::ckDispose(config)
CkStringBuilder::ckDispose(endpoint)
CkRest::ckDispose(rest)
CkAuthAzureStorage::ckDispose(azAuth)
CkSocket::ckDispose(connection)
CkXml::ckDispose(result)
CkFileAccess::ckDispose(input)
CkStream::ckDispose(source)
ProcedureReturn
EndIf
If CkRest::ckResponseStatusCode(rest) <> 201
Debug "HTTP status: " + Str(CkRest::ckResponseStatusCode(rest))
Debug "Azure error code: " + CkRest::ckResponseHdrByName(rest,"x-ms-error-code")
Debug "Request ID: " + CkRest::ckResponseHdrByName(rest,"x-ms-request-id")
; Inspect any error body privately; do not log credentials, SAS URLs or payloads.
CkStringBuilder::ckDispose(textCompare)
CkJsonObject::ckDispose(config)
CkStringBuilder::ckDispose(endpoint)
CkRest::ckDispose(rest)
CkAuthAzureStorage::ckDispose(azAuth)
CkSocket::ckDispose(connection)
CkXml::ckDispose(result)
CkFileAccess::ckDispose(input)
CkStream::ckDispose(source)
ProcedureReturn
EndIf
Debug "Blob created."
Debug "ETag: " + CkRest::ckResponseHdrByName(rest,"ETag")
CkStringBuilder::ckDispose(textCompare)
CkJsonObject::ckDispose(config)
CkStringBuilder::ckDispose(endpoint)
CkRest::ckDispose(rest)
CkAuthAzureStorage::ckDispose(azAuth)
CkSocket::ckDispose(connection)
CkXml::ckDispose(result)
CkFileAccess::ckDispose(input)
CkStream::ckDispose(source)
ProcedureReturn
EndProcedure