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

Azure Files: Download with Entra OAuth and Backup Intent

See more Azure Cloud Storage Examples

Use a Storage-audience token saved by either authentication example, then stream a file through FileREST with x-ms-file-request-intent: backup. The identity needs file read permission and readFileBackupSemantics; Storage File Data Privileged Reader is the built-in read role for this use. This privileged access can bypass file/directory ACLs within the role's scope. Ordinary SMB Share Reader permissions alone are insufficient.

This is REST authorization, not SMB/Kerberos authentication. It applies to classic Azure file shares created through Microsoft.Storage, not Microsoft.FileShares resources. A 403 can indicate missing backup-semantics permission or account/network policy even when token acquisition succeeds. The token is not refreshed automatically; reacquire it before expiry.

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(".file.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 = "/examples/files/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 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_header("x-ms-file-request-intent", "backup")
if not success then
    print("Could not prepare the request.")
    return
end

local files = chilkat.FileAccess.new()
if files:file_exists("qa_output/azure-file.bin") then
    print("Choose a new destination filename.")
    return
end

local sink = chilkat.Stream.new()
sink.sink_file = "qa_output/azure-file.bin.part"
success = rest:send_req_no_body("GET", resourcePath)
if not success then
    print("Could not prepare the request.")
    return
end

local httpStatus = rest:read_response_header()
if httpStatus < 0 then
    print("Could not read response headers.")
    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 = rest:read_resp_body_stream(sink, false)
if not success then
    print("Transfer incomplete; the .part file is not a completed download.")
    return
end

success = files:file_rename("qa_output/azure-file.bin.part", "qa_output/azure-file.bin")
if not success then
    print("Could not publish downloaded file.")
    return
end

print("File downloaded using Entra authorization.")