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

Azure Table: Preserve Existing Policies When Updating a Table ACL

See more Azure Table Service Examples

Reads the existing XML policy collection, inserts or updates only example-read, preserves the other entries, and writes the full collection. Disabled by default. Refuses duplicate IDs and a sixth policy. This changes any SAS associated with example-read; it sets read permission and a short validity window.

Set Table ACL replaces the complete collection and has no ETag compare-and-swap protection here. Coordinate administrators to avoid lost concurrent edits. A changed policy may take up to 30 seconds to propagate; do not interpret an immediate SAS 403 as proof that the update failed.

Targets Azure Storage Table service in the public cloud, not Azure Cosmos DB for Table. Uses Chilkat 11.6.0 or later and an unlocked library. Direct data requests pin x-ms-version 2026-04-06 and OData 3.0. Shared Key examples read private qa_data/azure/storage.json with account and access_key string fields. Authentication examples explain their additional fields. Create private local directories before running; keep credentials, tokens, SAS, entity values, checkpoints and diagnostics outside public content and source control.

HTTPS certificates are verified. Transport completion and HTTP status are checked separately. Writes are not automatically retried after an uncertain outcome. Entity keys, ETags and continuation tokens remain strings. Examples operate on the existing ExampleCustomers table unless noted; review the targets before adapting them. Microsoft Entra ID with suitable Table data roles is preferred where available.

Microsoft reference.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- StringBuilder compares text by value in every generated language.
local textCompare = chilkat.StringBuilder.new()
local applyChange = false
if not applyChange then
    print("This replaces the stored access-policy collection. Review the target and deliberately enable applyChange.")
    return
end

-- 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 accountText = chilkat.StringBuilder.new()
success = accountText:append(config:string_of("account"))
if not success then
    print("Could not prepare the request.")
    return
end

if (accountText.length < 3) or (accountText.length > 24) then
    print("Storage account names need 3-24 lowercase letters or digits.")
    return
end

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

local charIndex = 0
while charIndex < accountText.length do
    if not allowedAccountChars:contains(accountText:get_range(charIndex, 1, false), true) then
        print("Storage account names need lowercase letters or digits.")
        return
    end

    charIndex = charIndex + 1
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(".table.core.windows.net")
if not success then
    print("Could not prepare the request.")
    return
end

-- Table names contain only letters and digits and are case-insensitive.
-- Entity PartitionKey and RowKey values are case-sensitive.
local resourcePath = "/ExampleCustomers"
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 = "Table"
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 result = chilkat.JsonObject.new()
result.strict = true
success = rest:add_header("DataServiceVersion", "3.0;NetFx")
if not success then
    print("Could not prepare the request.")
    return
end

success = rest:add_header("MaxDataServiceVersion", "3.0;NetFx")
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("Accept", "application/xml")
if not success then
    print("Could not prepare the request.")
    return
end

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

local responseText = rest:full_request_no_body("GET", resourcePath)
if not rest.last_method_success then
    print("Request did not complete; inspect the target before retrying a write.")
    return
end

if rest.response_status_code ~= 200 then
    print("HTTP status: " .. rest.response_status_code)
    print("Request ID: " .. rest:response_hdr_by_name("x-ms-request-id"))
    -- 404 is missing, 409 is conflict, and 412 is a stale ETag: none are success.
    -- Keep response bodies private. Reconcile uncertain writes before retrying.
    return
end

local policies = chilkat.Xml.new()
policies.strict = true
success = policies:load_xml(responseText)
if not success then
    print("Invalid policy XML.")
    return
end

textCompare:clear()
textCompare:set_string(policies.tag)
local sameText3 = textCompare:contents_equal("SignedIdentifiers", true)
if not sameText3 then
    print("Unexpected policy root.")
    return
end

local policyCount = policies:num_children_having_tag("SignedIdentifier")
if policyCount > 5 then
    print("Unexpected access policy count.")
    return
end

local policyIds = chilkat.Hashtable.new()
local policyIndex = 0
local editIndex = -1
while policyIndex < policyCount do
    policies.i = policyIndex
    local policyId = policies:get_child_content("SignedIdentifier[i]|Id")
    textCompare:clear()
    textCompare:set_string(policyId)
    local sameText4 = textCompare:contents_equal("", true)
    if sameText4 then
        print("Missing policy ID.")
        return
    end

    if policyIds:contains(policyId) then
        print("Duplicate policy ID.")
        return
    end

    success = policyIds:add_str(policyId, "seen")
    if not success then
        print("Could not prepare the request.")
        return
    end

    textCompare:clear()
    textCompare:set_string(policyId)
    local sameText5 = textCompare:contents_equal("example-read", true)
    if sameText5 then
        editIndex = policyIndex
    end

    policyIndex = policyIndex + 1
end

if editIndex < 0 then
    if policyCount >= 5 then
        print("All five policy slots are in use; do not remove an unrelated policy.")
        return
    end

    editIndex = policyCount
end

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

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

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

success = policyExpiry:add_seconds(900)
if not success then
    print("Could not prepare the request.")
    return
end

policies.i = editIndex
policies:update_child_content("SignedIdentifier[i]|Id", "example-read")
policies:update_child_content("SignedIdentifier[i]|AccessPolicy|Start", policyStart:get_as_timestamp(false))
policies:update_child_content("SignedIdentifier[i]|AccessPolicy|Expiry", policyExpiry:get_as_timestamp(false))
policies:update_child_content("SignedIdentifier[i]|AccessPolicy|Permission", "r")
rest:disconnect(10)
success = connection:connect(endpoint:get_as_string(), 443, true, 20000)
if not success then
    print("TLS reconnection failed.")
    return
end

success = rest:use_connection(connection, false)
if not success then
    print("Could not prepare the request.")
    return
end

responseText = rest:full_request_string("PUT", resourcePath, policies:get_xml())
if not rest.last_method_success then
    print("Request did not complete; inspect the target before retrying a write.")
    return
end

if rest.response_status_code ~= 204 then
    print("HTTP status: " .. rest.response_status_code)
    print("Request ID: " .. rest:response_hdr_by_name("x-ms-request-id"))
    -- 404 is missing, 409 is conflict, and 412 is a stale ETag: none are success.
    -- Keep response bodies private. Reconcile uncertain writes before retrying.
    return
end

print("Stored policy updated; allow time for propagation.")