Azure Blob: List Versions and Recover Deleted Content
See more Azure Cloud Storage Examples
For a flat-namespace account, list the versions, snapshots and soft-deleted entries for one blob. Keep version IDs and snapshot timestamps as strings. The default action only lists choices; change action deliberately to undelete, restore-version or restore-snapshot and supply the selected ID.
Undelete recovers eligible soft-deleted content within its retention period. When versioning is enabled, undelete does not automatically make an older version the current base blob: restore the selected live version by copying it to the base name. A deleted snapshot/version may first require undelete. Restoring over an existing base requires its current quoted ETag; otherwise this example only creates an absent base. Retention expiration, permanent deletion and immutability policies can prevent recovery. Hierarchical-namespace accounts use a different recovery workflow with deletion IDs.
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.
Chilkat Lua Downloads
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"
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 responseText = ""
local result = chilkat.Xml.new()
result.strict = true
-- Flat-namespace Blob account. Default is inspection only.
-- Choose list, undelete, restore-version, or restore-snapshot.
local action = "list"
local selectedId = ""
-- For restoring over an existing base blob, supply its current quoted ETag.
-- Empty means restore only when the base blob is absent.
local destinationEtag = ""
success = rest:add_query_param("restype", "container")
if not success then
print("Could not prepare the request.")
return
end
success = rest:add_query_param("comp", "list")
if not success then
print("Could not prepare the request.")
return
end
success = rest:add_query_param("prefix", "example.bin")
if not success then
print("Could not prepare the request.")
return
end
success = rest:add_query_param("include", "versions,snapshots,deleted,deletedwithversions")
if not success then
print("Could not prepare the request.")
return
end
success = rest:add_query_param("maxresults", "1000")
if not success then
print("Could not prepare the request.")
return
end
local seenMarkers = chilkat.Hashtable.new()
local marker = ""
local more = true
local page = 0
while more do
if page >= 1000 then
print("Page limit reached; results are incomplete. Save the continuation marker.")
return
end
responseText = rest:full_request_no_body("GET", 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
success = result:load_xml(responseText)
if not success then
print("Expected well-formed XML.")
return
end
if not result:tag_equals("EnumerationResults") then
print("Unexpected XML response root.")
return
end
local count = result:num_children_at("Blobs")
local i = 0
while i < count do
result.i = i
textCompare:clear()
textCompare:set_string(result:get_child_content("Blobs|Blob[i]|Name"))
local sameText3 = textCompare:contents_equal("example.bin", true)
if sameText3 then
print("Version ID: " .. result:get_child_content("Blobs|Blob[i]|VersionId"))
print("Snapshot: " .. result:get_child_content("Blobs|Blob[i]|Snapshot"))
print("Deleted: " .. result:get_child_content("Blobs|Blob[i]|Deleted"))
print("Current version: " .. result:get_child_content("Blobs|Blob[i]|IsCurrentVersion"))
print("Has versions only: " .. result:get_child_content("Blobs|Blob[i]|HasVersionsOnly"))
end
i = i + 1
end
marker = result:get_child_content("NextMarker")
page = page + 1
textCompare:clear()
textCompare:set_string(marker)
local sameText4 = textCompare:contents_equal("", true)
if sameText4 then
more = false
else
if seenMarkers:contains(marker) then
print("Repeated marker; results are incomplete.")
return
end
success = seenMarkers:add_int(marker, 1)
if not success then
print("Could not prepare the request.")
return
end
if page > 1 then
success = rest:remove_query_param("marker")
if not success then
print("Could not prepare the request.")
return
end
end
success = rest:add_query_param("marker", marker)
if not success then
print("Could not prepare the request.")
return
end
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
end
end
textCompare:clear()
textCompare:set_string(action)
local sameText5 = textCompare:contents_equal("list", true)
if not sameText5 then
success = rest:clear_all_query_params()
if not success then
print("Could not prepare the request.")
return
end
resourcePath = "/examplecontainer/example.bin"
textCompare:clear()
textCompare:set_string(action)
local sameText6 = textCompare:contents_equal("undelete", true)
if sameText6 then
success = rest:add_query_param("comp", "undelete")
if not success then
print("Could not prepare the request.")
return
end
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("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 ~= 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
print("Undelete accepted. Rerun listing before selecting a version or snapshot.")
else
textCompare:clear()
textCompare:set_string(action)
local sameText7 = textCompare:contents_equal("restore-version", true)
textCompare:clear()
textCompare:set_string(action)
local sameText8 = textCompare:contents_equal("restore-snapshot", true)
if (not sameText7) and (not sameText8) then
print("Unknown recovery action.")
return
end
textCompare:clear()
textCompare:set_string(selectedId)
local sameText9 = textCompare:contents_equal("", true)
if sameText9 then
print("Select a version or snapshot ID from the listing first.")
return
end
local encodedId = chilkat.StringBuilder.new()
success = encodedId:append(selectedId)
if not success then
print("Could not prepare the request.")
return
end
success = encodedId:encode("url", "utf-8")
if not success then
print("Could not prepare the request.")
return
end
local restoreUrl = chilkat.StringBuilder.new()
success = restoreUrl:append("https://")
if not success then
print("Could not prepare the request.")
return
end
success = restoreUrl:append(endpoint:get_as_string())
if not success then
print("Could not prepare the request.")
return
end
success = restoreUrl:append(resourcePath)
if not success then
print("Could not prepare the request.")
return
end
textCompare:clear()
textCompare:set_string(action)
local sameText10 = textCompare:contents_equal("restore-version", true)
if sameText10 then
success = restoreUrl:append("?versionid=")
if not success then
print("Could not prepare the request.")
return
end
else
success = restoreUrl:append("?snapshot=")
if not success then
print("Could not prepare the request.")
return
end
end
success = restoreUrl:append(encodedId:get_as_string())
if not success then
print("Could not prepare the request.")
return
end
success = rest:add_header("x-ms-copy-source", restoreUrl:get_as_string())
if not success then
print("Could not prepare the request.")
return
end
textCompare:clear()
textCompare:set_string(destinationEtag)
local sameText11 = textCompare:contents_equal("", true)
if sameText11 then
success = rest:add_header("If-None-Match", "*")
if not success then
print("Could not prepare the request.")
return
end
else
success = rest:add_header("If-Match", destinationEtag)
if not success then
print("Could not prepare the request.")
return
end
end
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("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 sameText12 = textCompare:contents_equal("", true)
if sameText12 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 sameText13 = textCompare:contents_equal(copyId, true)
if not sameText13 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 sameText14 = textCompare:contents_equal("success", true)
if sameText14 then
completed = true
else
textCompare:clear()
textCompare:set_string(copyStatus)
local sameText15 = textCompare:contents_equal("pending", true)
if not sameText15 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.")
end
end
print("Recovery workflow finished.")