Azure Blob: Upload Using a Service SAS
See more Azure Cloud Storage Examples
Read the private query token produced by the corresponding SAS-generation example and upload a small binary file to its matching account/path. Do not add a Shared Key Authorization header. If-None-Match: * prevents accidental overwrite; remove it only for intentional replacement. The same 5,000 MiB single-request service limit applies, but this example buffers input and is intended for small files.
Keep query values encoded exactly once. A 403 can mean expiry, start-time skew, wrong path, insufficient permissions, network restrictions or Shared Key being disabled. SAS is a bearer credential; do not print the URL or token.
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 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
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 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
success = rest:add_header("x-ms-version", "2026-04-06")
if not success then
print("Could not prepare the request.")
return
end
local token = chilkat.StringBuilder.new()
success = token:load_file("qa_data/azure/blob-sas.txt", "utf-8")
if not success then
print("Could not load private SAS.")
return
end
success = token:trim()
if not success then
print("Could not prepare the request.")
return
end
if token.length == 0 then
print("SAS file is empty.")
return
end
-- Load the raw query token; do not pre-encode it or add Shared Key authorization.
success = rest:add_query_params(token:get_as_string())
if not success then
print("Could not prepare the request.")
return
end
success = rest:add_header("x-ms-blob-type", "BlockBlob")
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
success = rest:add_header("Content-Type", "application/octet-stream")
if not success then
print("Could not prepare the request.")
return
end
local data = chilkat.BinData.new()
success = data:load_file("qa_data/bin/sample.bin")
if not success then
print("Could not read upload input.")
return
end
responseBody:clear()
success = rest:full_request_bd("PUT", resourcePath, data, responseBody)
if not 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("Blob created using SAS.")