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

Azure Blob: Copy a Blob and Track Completion

See more Azure Cloud Storage Examples

Copy a blob within the same account without downloading it into the application. Shared Key authorizes the same-account source and destination. If-None-Match: * prevents replacing an existing destination. Cross-account sources need separate authorization, often a read SAS; never log that URL.

A 202 means accepted, not completed. Retain the returned copy ID and poll destination properties until success, failure or abort. The example waits for at most 20 polls; pending at that point is not success and does not cancel the copy. On restart continue tracking the original ID, rather than issuing another copy. Account type, archive tier, leases and destination conditions can affect copy eligibility.

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

-- 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-copy.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 result = chilkat.Xml.new()
result.strict = true
local sourceUrl = chilkat.StringBuilder.new()
success = sourceUrl:append("https://")
if not success then
    print("Could not prepare the request.")
    return
end

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

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

success = rest:add_header("x-ms-copy-source", sourceUrl:get_as_string())
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_no_body("PUT", resourcePath)
if not rest.last_method_success then
    print("Request did not complete; outcome may be uncertain.")
    return
end

if rest.response_status_code ~= 202 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

local copyId = rest:response_hdr_by_name("x-ms-copy-id")
textCompare:clear()
textCompare:set_string(copyId)
local sameText3 = textCompare:contents_equal("", true)
if sameText3 then
    print("Copy accepted without a copy ID; inspect before retrying.")
    return
end

print("Copy ID: " .. copyId)
-- Save copyId and destination in application state if polling must survive restart.
success = rest:clear_all_headers()
if not success then
    print("Could not prepare the request.")
    return
end

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

local polls = 0
local completed = false
while (polls < 20) and (not completed) do
    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_no_body("HEAD", resourcePath)
    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

    textCompare:clear()
    textCompare:set_string(rest:response_hdr_by_name("x-ms-copy-id"))
    local sameText4 = textCompare:contents_equal(copyId, true)
    if not sameText4 then
        print("Destination no longer reports this copy ID.")
        return
    end

    local copyStatus = rest:response_hdr_by_name("x-ms-copy-status")
    textCompare:clear()
    textCompare:set_string(copyStatus)
    local sameText5 = textCompare:contents_equal("success", true)
    if sameText5 then
        completed = true
    else
        textCompare:clear()
        textCompare:set_string(copyStatus)
        local sameText6 = textCompare:contents_equal("pending", true)
        if not sameText6 then
            print("Copy failed, was aborted, or returned an unexpected status; inspect copy diagnostics privately.")
            return
        end

        -- Pause between polls; the copy can outlast this example's bounded wait.
        connection:sleep_ms(1000)
    end

    polls = polls + 1
end

if not completed then
    print("Copy is still pending. Continue polling this ID; do not start another copy.")
    return
end

print("Copy completed.")