Azure Blob: Upload Blocks with Content-MD5 and Commit
See more Azure Cloud Storage Examples
Stage 4 MiB blocks and commit them in order. The current service limits are 4,000 MiB per block and 50,000 committed blocks; this example chooses smaller blocks to bound memory. Every block ID has the same decoded length, includes a per-upload prefix, and is passed as the lowercase blockid query parameter through AddQueryParam for correct escaping.
Only the commit makes the new content visible. If-None-Match on commit prevents replacing an existing blob. A failed staging or commit can leave uncommitted blocks; this sample is not a checkpoint/resume implementation. Prevent concurrent writers and do not change the source while uploading. An empty input commits an empty list.
Content-MD5 is the Base64 MD5 of each block, not hexadecimal. It verifies transport of that block and is removed before committing XML. It does not automatically become the full blob content MD5.
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 put block list reference.
Chilkat Lua Downloads
local chilkat = require("chilkat")
-- StringBuilder compares text by value in every generated language.
local textCompare = chilkat.StringBuilder.new()
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local success = false
local config = chilkat.JsonObject.new()
config.strict = true
success = config:load_file("qa_data/azure/storage.json")
if not success then
print("Could not load private Azure configuration.")
return
end
textCompare:clear()
textCompare:set_string(config:string_of("account"))
local sameText1 = textCompare:contents_equal("", true)
if (config:json_type_of("account") ~= 1) or (sameText1) then
print("Supply the storage account name.")
return
end
-- access_key is the base64 account key, not a connection string or SAS.
textCompare:clear()
textCompare:set_string(config:string_of("access_key"))
local sameText2 = textCompare:contents_equal("", true)
if (config:json_type_of("access_key") ~= 1) or (sameText2) then
print("Supply the base64 account access key.")
return
end
local endpoint = chilkat.StringBuilder.new()
success = endpoint:append(config:string_of("account"))
if not success then
print("Could not prepare the request.")
return
end
success = endpoint:append(".blob.core.windows.net")
if not success then
print("Could not prepare the request.")
return
end
-- resourcePath is already URL-encoded. Encode each path segment once when adapting it.
-- Keep slash separators; names and paths are case-sensitive.
local resourcePath = "/examplecontainer/example.bin"
local rest = chilkat.Rest.new()
rest.connect_timeout_ms = 20000
rest.idle_timeout_ms = 60000
local azAuth = chilkat.AuthAzureStorage.new()
azAuth.account = config:string_of("account")
azAuth.access_key = config:string_of("access_key")
azAuth.service = "Blob"
azAuth.scheme = "SharedKey"
azAuth.xms_version = "2026-04-06"
success = rest:set_auth_azure_storage(azAuth)
if not success then
print("Could not configure Shared Key signing.")
return
end
local connection = chilkat.Socket.new()
connection.require_ssl_cert_verify = true
connection.max_read_idle_ms = 60000
connection.max_send_idle_ms = 60000
success = connection:connect(endpoint:get_as_string(), 443, true, 20000)
if not success then
print("TLS connection failed; inspect diagnostics privately.")
return
end
-- Do not replay a write automatically after losing its response.
success = rest:use_connection(connection, false)
if not success then
print("Could not prepare the request.")
return
end
local responseBody = chilkat.StringBuilder.new()
local result = chilkat.Xml.new()
result.strict = true
local input = chilkat.FileAccess.new()
success = input:open_for_read("qa_data/bin/sample.bin")
if not success then
print("Could not open input.")
input:file_close()
return
end
local blockSize = 4194304
local numBlocks = input:get_num_blocks(blockSize)
if (numBlocks < 0) or (numBlocks > 50000) then
print("Input exceeds the demonstration block limit.")
input:file_close()
return
end
local random = chilkat.Crypt2.new()
local uploadId = random:generate_uuid()
if not random.last_method_success then
print("Could not create upload ID.")
input:file_close()
return
end
-- Persist uploadId, block IDs and the unchanged source fingerprint to implement resumption.
local blocks = chilkat.Xml.new()
blocks.tag = "BlockList"
local chunk = chilkat.BinData.new()
local blockText = chilkat.StringBuilder.new()
local i = 0
while i < numBlocks do
success = chunk:clear()
if not success then
print("Could not prepare the request.")
input:file_close()
return
end
success = input:read_block_bd(i, blockSize, chunk)
if not success then
print("Failed to read block.")
input:file_close()
return
end
blockText:clear()
success = blockText:append(uploadId)
if not success then
print("Could not prepare the request.")
input:file_close()
return
end
success = blockText:append(input:gen_block_id(i, 5, "ascii"))
if not success then
print("Could not prepare the request.")
input:file_close()
return
end
success = blockText:encode("base64", "utf-8")
if not success then
print("Could not prepare the request.")
input:file_close()
return
end
local blockId = blockText:get_as_string()
success = rest:clear_all_query_params()
if not success then
print("Could not prepare the request.")
input:file_close()
return
end
success = rest:add_query_param("comp", "block")
if not success then
print("Could not prepare the request.")
input:file_close()
return
end
success = rest:add_query_param("blockid", blockId)
if not success then
print("Could not prepare the request.")
input:file_close()
return
end
local digest = chunk:get_hash("md5", "base64")
if not chunk.last_method_success then
print("Could not hash block.")
input:file_close()
return
end
success = rest:add_header("Content-MD5", digest)
if not success then
print("Could not prepare the request.")
input:file_close()
return
end
responseBody:clear()
success = rest:full_request_bd("PUT", resourcePath, chunk, responseBody)
if not success then
print("Request did not complete; outcome may be uncertain.")
input:file_close()
return
end
if rest.response_status_code ~= 201 then
print("HTTP status: " .. rest.response_status_code)
print("Azure error code: " .. rest:response_hdr_by_name("x-ms-error-code"))
print("Request ID: " .. rest:response_hdr_by_name("x-ms-request-id"))
-- Inspect any error body privately; do not log credentials, SAS URLs or payloads.
input:file_close()
return
end
-- Add only a successfully staged block, in source order.
blocks:new_child2("Uncommitted", blockId)
i = i + 1
rest:disconnect(10)
success = connection:connect(endpoint:get_as_string(), 443, true, 20000)
if not success then
print("TLS reconnection failed.")
input:file_close()
return
end
success = rest:use_connection(connection, false)
if not success then
print("Could not prepare the request.")
input:file_close()
return
end
end
input:file_close()
success = rest:clear_all_query_params()
if not success then
print("Could not prepare the request.")
return
end
success = rest:add_query_param("comp", "blocklist")
if not success then
print("Could not prepare the request.")
return
end
success = rest:remove_header("Content-MD5")
if not success then
print("Could not prepare the request.")
return
end
success = rest:add_header("Content-Type", "application/xml")
if not success then
print("Could not prepare the request.")
return
end
success = rest:add_header("x-ms-blob-content-type", "application/octet-stream")
if not success then
print("Could not prepare the request.")
return
end
success = rest:add_header("If-None-Match", "*")
if not success then
print("Could not prepare the request.")
return
end
local responseText = rest:full_request_string("PUT", resourcePath, blocks:get_xml())
if not rest.last_method_success then
print("Request did not complete; outcome may be uncertain.")
return
end
if rest.response_status_code ~= 201 then
print("HTTP status: " .. rest.response_status_code)
print("Azure error code: " .. rest:response_hdr_by_name("x-ms-error-code"))
print("Request ID: " .. rest:response_hdr_by_name("x-ms-request-id"))
-- Inspect any error body privately; do not log credentials, SAS URLs or payloads.
return
end
print("Block list committed; blob created.")