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

Azure Files: Create and Upload a Small File

See more Azure Cloud Storage Examples

Create or replace a file in an existing SMB share/directory, then write one range of up to 4 MiB. The inclusive range end is start + byte count - 1. Empty input creates a zero-length file without issuing an invalid range. Every local read and HTTP result is checked.

Replacement is disabled by default. Create File can truncate an existing file. Range writes are not an atomic whole-file upload; failure leaves a partially written target. Coordinate writers and use a temporary remote name plus a later publication workflow where appropriate. The sample deliberately bounds int offsets to 2,000,000,000 bytes and rejects larger files before creation; this is not Azure's file-size limit. API versions 2026-02-06 and later additionally support up to 4 MiB in Create File itself; the separate create/range pattern remains supported.

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.

Microsoft put range reference.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- StringBuilder compares text by value in every generated language.
local textCompare = chilkat.StringBuilder.new()
local success = false
local applyChange = false
if not applyChange then
    print("File creation/replacement disabled. Review the target and set applyChange to true deliberately.")
    return
end

-- Assumes Chilkat is unlocked. See Global Unlock Sample.
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(".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 azAuth = chilkat.AuthAzureStorage.new()
azAuth.account = config:string_of("account")
azAuth.access_key = config:string_of("access_key")
azAuth.service = "File"
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 responseBody = chilkat.StringBuilder.new()
local result = chilkat.Xml.new()
result.strict = true
local input = chilkat.FileAccess.new()
local fileSize = input:file_size_str("qa_data/bin/sample.bin")
if not input.last_method_success then
    print("Could not get input size.")
    return
end

-- This demonstration uses int offsets: reject sizes outside its explicit 2,000,000,000-byte bound.
-- The bound is local sample arithmetic, not the Azure Files service size limit.
local sizeText = chilkat.StringBuilder.new()
success = sizeText:append(fileSize)
if not success then
    print("Could not prepare the request.")
    return
end

if sizeText.length > 10 then
    print("Input exceeds the demonstration size bound.")
    return
end

local sizeValue = chilkat.JsonObject.new()
success = sizeValue:update_number("size", fileSize)
if not success then
    print("Could not prepare the request.")
    return
end

-- Compare the decimal text before converting to a 32-bit int.
if (sizeText.length == 10) and ((not sizeText:starts_with("1", true)) and (not sizeText:contents_equal("2000000000", true))) then
    print("Input exceeds the demonstration size bound.")
    return
end

local total = sizeValue:int_of("size")
if total > 4194304 then
    print("Use the multi-range example for inputs larger than 4 MiB.")
    return
end

success = input:open_for_read("qa_data/bin/sample.bin")
if not success then
    print("Could not open input.")
    input:file_close()
    return
end

success = rest:add_header("x-ms-type", "file")
if not success then
    print("Could not prepare the request.")
    input:file_close()
    return
end

success = rest:add_header("x-ms-content-length", fileSize)
if not success then
    print("Could not prepare the request.")
    input:file_close()
    return
end

success = rest:add_header("x-ms-file-permission", "inherit")
if not success then
    print("Could not prepare the request.")
    input:file_close()
    return
end

success = rest:add_header("x-ms-file-attributes", "None")
if not success then
    print("Could not prepare the request.")
    input:file_close()
    return
end

success = rest:add_header("x-ms-file-creation-time", "now")
if not success then
    print("Could not prepare the request.")
    input:file_close()
    return
end

success = rest:add_header("x-ms-file-last-write-time", "now")
if not success then
    print("Could not prepare the request.")
    input:file_close()
    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.")
    input:file_close()
    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.
    input:file_close()
    return
end

-- Creation/replacement reserves the final length; subsequent ranges are separate operations.
success = rest:clear_all_headers()
if not success then
    print("Could not prepare the request.")
    input:file_close()
    return
end

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

success = rest:add_header("x-ms-write", "update")
if not success then
    print("Could not prepare the request.")
    input:file_close()
    return
end

local chunk = chilkat.BinData.new()
local range = chilkat.StringBuilder.new()
local offset = 0
while offset < total do
    local count = total - offset
    if count > 4194304 then
        count = 4194304
    end

    success = chunk:clear()
    if not success then
        print("Could not prepare the request.")
        input:file_close()
        return
    end

    success = input:file_read_bd(count, chunk)
    if not success then
        print("Input read failed.")
        input:file_close()
        return
    end

    if chunk.num_bytes ~= count then
        print("Input changed or a short read occurred.")
        input:file_close()
        return
    end

    range:clear()
    success = range:append("bytes=")
    if not success then
        print("Could not prepare the request.")
        input:file_close()
        return
    end

    success = range:append_int(offset)
    if not success then
        print("Could not prepare the request.")
        input:file_close()
        return
    end

    success = range:append("-")
    if not success then
        print("Could not prepare the request.")
        input:file_close()
        return
    end

    success = range:append_int(offset + count - 1)
    if not success then
        print("Could not prepare the request.")
        input:file_close()
        return
    end

    success = rest:add_header("x-ms-range", range:get_as_string())
    if not success then
        print("Could not prepare the request.")
        input:file_close()
        return
    end

    rest:disconnect(10)
    success = connection:connect(endpoint:get_as_string(), 443, true, 20000)
    if not success then
        print("TLS reconnection failed.")
        input:file_close()
        return
    end

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

    responseBody:clear()
    success = rest:full_request_bd("PUT", resourcePath, chunk, responseBody)
    if not success then
        print("Request did not complete; outcome may be uncertain.")
        input:file_close()
        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.
        input:file_close()
        return
    end

    offset = offset + count
end

input:file_close()
print("File content uploaded.")