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

Azure Blob: Create a Read-Only User Delegation SAS

See more Azure Cloud Storage Examples

Use the Storage token saved by either authentication example to obtain a user delegation key and create an HTTPS-only read SAS for examplecontainer/example.bin. The principal needs generateUserDelegationKey permission at storage-account scope (or above), plus read access to the blob; Storage Blob Delegator alone does not grant content access. SAS permissions cannot exceed the principal's effective permissions.

Uses the returned key validity timestamps, with a requested start five minutes ago and expiry fifteen minutes ahead. User delegation keys cannot exceed seven days. Respect stricter account SAS-expiration policies. Save the complete URL privately; anyone holding this ordinary bearer SAS can use it until expiry. This example does not enable optional user-bound SAS, signed headers/query restrictions, or stored access policies (unsupported for user delegation SAS).

First obtain the token with Entra client credentials or an Azure VM managed identity.

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.

Microsoft reference.

Chilkat Lua Downloads

Lua
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 = "/"
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 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

-- Obtain this private file with either authentication example. Reacquire an expired token.
local savedToken = chilkat.JsonObject.new()
savedToken.strict = true
success = savedToken:load_file("qa_data/azure/storage-token.json")
if not success then
    print("Could not load Storage token.")
    return
end

textCompare:clear()
textCompare:set_string(savedToken:string_of("access_token"))
local sameText2 = textCompare:contents_equal("", true)
if (savedToken:json_type_of("access_token") ~= 1) or (sameText2) then
    print("Missing access token.")
    return
end

local expiry = chilkat.DateTime.new()
local now = chilkat.DateTime.new()
success = expiry:set_from_timestamp(savedToken:string_of("expires_at"))
if not success then
    print("Invalid token expiry.")
    return
end

success = now:set_from_current_system_time()
if not success then
    print("Could not prepare the request.")
    return
end

if expiry:diff_seconds(now) < 60 then
    print("Acquire a new Storage token before continuing.")
    return
end

local accessToken = savedToken:string_of("access_token")
local bearer = chilkat.StringBuilder.new()
success = bearer:append("Bearer ")
if not success then
    print("Could not prepare the request.")
    return
end

success = bearer:append(accessToken)
if not success then
    print("Could not prepare the request.")
    return
end

success = rest:add_header("Authorization", bearer:get_as_string())
if not success then
    print("Could not prepare the request.")
    return
end

success = rest:add_query_param("restype", "service")
if not success then
    print("Could not prepare the request.")
    return
end

success = rest:add_query_param("comp", "userdelegationkey")
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

local window = chilkat.DateTime.new()
success = window:set_from_current_system_time()
if not success then
    print("Could not prepare the request.")
    return
end

success = window:add_seconds(-300)
if not success then
    print("Could not prepare the request.")
    return
end

local signedStart = window:get_as_timestamp(false)
success = window:add_seconds(1200)
if not success then
    print("Could not prepare the request.")
    return
end

local signedExpiry = window:get_as_timestamp(false)
local keyRequest = chilkat.Xml.new()
keyRequest.tag = "KeyInfo"
keyRequest:new_child2("Start", signedStart)
keyRequest:new_child2("Expiry", signedExpiry)
local responseText = rest:full_request_string("POST", resourcePath, keyRequest: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 ~= 200 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

success = result:load_xml(responseText)
if not success then
    print("Expected well-formed XML.")
    return
end

if not result:tag_equals("UserDelegationKey") then
    print("Unexpected XML response root.")
    return
end

textCompare:clear()
textCompare:set_string(result:get_child_content("SignedOid"))
local sameText3 = textCompare:contents_equal("", true)
if sameText3 then
    print("Incomplete delegation-key response.")
    return
end

textCompare:clear()
textCompare:set_string(result:get_child_content("SignedTid"))
local sameText4 = textCompare:contents_equal("", true)
if sameText4 then
    print("Incomplete delegation-key response.")
    return
end

textCompare:clear()
textCompare:set_string(result:get_child_content("SignedStart"))
local sameText5 = textCompare:contents_equal("", true)
if sameText5 then
    print("Incomplete delegation-key response.")
    return
end

textCompare:clear()
textCompare:set_string(result:get_child_content("SignedExpiry"))
local sameText6 = textCompare:contents_equal("", true)
if sameText6 then
    print("Incomplete delegation-key response.")
    return
end

textCompare:clear()
textCompare:set_string(result:get_child_content("SignedService"))
local sameText7 = textCompare:contents_equal("", true)
if sameText7 then
    print("Incomplete delegation-key response.")
    return
end

textCompare:clear()
textCompare:set_string(result:get_child_content("SignedVersion"))
local sameText8 = textCompare:contents_equal("", true)
if sameText8 then
    print("Incomplete delegation-key response.")
    return
end

textCompare:clear()
textCompare:set_string(result:get_child_content("Value"))
local sameText9 = textCompare:contents_equal("", true)
if sameText9 then
    print("Incomplete delegation-key response.")
    return
end

textCompare:clear()
textCompare:set_string(result:get_child_content("SignedService"))
local sameText10 = textCompare:contents_equal("b", true)
if not sameText10 then
    print("Expected a Blob delegation key.")
    return
end

local sas = chilkat.AuthAzureSAS.new()
-- The returned key Value is base64. It is not the storage account key.
sas.access_key = result:get_child_content("Value")
-- 2026-04-06 layout includes the optional user binding and signed request fields.
-- They remain empty here, but their LF-separated positions must still exist.
sas.string_to_sign = "sp,st,se,resource,skoid,sktid,skt,ske,sks,skv,saoid,suoid,scid,skdutid,sduoid,sip,spr,sv,sr,snapshot,ses,headers,query,rscc,rscd,rsce,rscl,rsct"
local canonical = chilkat.StringBuilder.new()
success = canonical:append("/blob/")
if not success then
    print("Could not prepare the request.")
    return
end

success = canonical:append(config:string_of("account"))
if not success then
    print("Could not prepare the request.")
    return
end

success = canonical:append("/examplecontainer/example.bin")
if not success then
    print("Could not prepare the request.")
    return
end

success = sas:set_non_token_param("resource", canonical:get_as_string())
if not success then
    print("Could not prepare the request.")
    return
end

success = sas:set_token_param("skoid", "skoid", result:get_child_content("SignedOid"))
if not success then
    print("Could not prepare the request.")
    return
end

success = sas:set_token_param("sktid", "sktid", result:get_child_content("SignedTid"))
if not success then
    print("Could not prepare the request.")
    return
end

success = sas:set_token_param("skt", "skt", result:get_child_content("SignedStart"))
if not success then
    print("Could not prepare the request.")
    return
end

success = sas:set_token_param("ske", "ske", result:get_child_content("SignedExpiry"))
if not success then
    print("Could not prepare the request.")
    return
end

success = sas:set_token_param("sks", "sks", result:get_child_content("SignedService"))
if not success then
    print("Could not prepare the request.")
    return
end

success = sas:set_token_param("skv", "skv", result:get_child_content("SignedVersion"))
if not success then
    print("Could not prepare the request.")
    return
end

success = sas:set_token_param("sp", "sp", "r")
if not success then
    print("Could not prepare the request.")
    return
end

success = sas:set_token_param("st", "st", result:get_child_content("SignedStart"))
if not success then
    print("Could not prepare the request.")
    return
end

success = sas:set_token_param("se", "se", result:get_child_content("SignedExpiry"))
if not success then
    print("Could not prepare the request.")
    return
end

success = sas:set_token_param("spr", "spr", "https")
if not success then
    print("Could not prepare the request.")
    return
end

success = sas:set_token_param("sv", "sv", "2026-04-06")
if not success then
    print("Could not prepare the request.")
    return
end

success = sas:set_token_param("sr", "sr", "b")
if not success then
    print("Could not prepare the request.")
    return
end

local sasToken = sas:generate_token()
if not sas.last_method_success then
    print("SAS generation failed.")
    return
end

local sasUrl = chilkat.StringBuilder.new()
success = sasUrl:append("https://")
if not success then
    print("Could not prepare the request.")
    return
end

success = sasUrl:append(endpoint:get_as_string())
if not success then
    print("Could not prepare the request.")
    return
end

success = sasUrl:append("/examplecontainer/example.bin?")
if not success then
    print("Could not prepare the request.")
    return
end

success = sasUrl:append(sasToken)
if not success then
    print("Could not prepare the request.")
    return
end

local files = chilkat.FileAccess.new()
success = files:write_entire_text_file("qa_data/azure/read-blob-url.txt", sasUrl:get_as_string(), "utf-8", false)
if not success then
    print("Could not save the SAS URL privately.")
    return
end

print("Read-only blob SAS URL saved privately; it expires in approximately 15 minutes.")